AcWing 29. 删除链表中重复的节点
原题链接
简单
作者:
Saber__
,
2024-11-03 20:10:36
,
所有人可见
,
阅读 2
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* head) {
auto dummy = new ListNode(-1);
dummy->next = head;
ListNode* p = dummy;
while(p->next)
{
ListNode* q = p->next;
while(q->next && q->next->val == p->next->val) q = q->next;
if(p->next == q) p =q;
else p->next = q->next;
}
return dummy->next;
}
};