remove-duplicates-from-sorted-list (刪除)
阿新 • • 發佈:2018-11-21
bsp include rom 思路 while int ica space from
題意略:
思路:先造一個點它與所有點的值都不同,那麽只要後面兩個點的值相同就開始判斷後面是不是也相同,最後將相同的拆下來就可以了。
#include<iostream> #include<cstdio> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: ListNode *deleteDuplicates(ListNode *head) { ListNode*pre = new ListNode(666); pre->next = head; ListNode *p = pre, *q; while (p->next&&p->next->next){ if (p->next->val == p->next->next->val){ q = p->next->next->next; while (q&&q->val == p->next->val)q = q->next; p->next->next = q; } else{ p = p->next; } } return pre->next; } };
remove-duplicates-from-sorted-list (刪除)