Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 82. Remove Duplicates from Sorted List II
題目:

解答:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode ans;
ans.next = head;
ListNode *pre = &ans;
ListNode *cur = head;
while (cur) {
if (cur->next && cur->val == cur->next->val) {
int v = cur->val;
while (cur && cur->val == v) {
cur = cur->next;
}
pre->next = cur;
}
else {
pre = cur;
cur = cur->next;
}
}
return ans.next;
}
};