LeetCode 82. 删除排序链表中的重复元素 II
原题链接
中等
作者:
张智豪
,
2021-04-27 22:07:19
,
所有人可见
,
阅读 240
/**
* 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* dummy = new ListNode(0);
dummy->next = head;
ListNode* p = dummy;
while (p->next)
{
ListNode* q = p->next;
while (q && q->val == p->next->val)
q = q->next;
if (p->next->next == q) p = p->next;
else p->next = q;
}
return dummy->next;
}
};