AcWing 29. 删除链表中重复的节点
原题链接
中等
作者:
吴子涵
,
2021-07-17 10:49:21
,
所有人可见
,
阅读 218
/**
* 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) {
if(head==NULL)return head;
auto s=new ListNode(-1);
s->next = head;
auto p=s;
while(p->next)
{
auto t=p->next,u=p->next;
while(t&&t->val==p->next->val)t=t->next;
if(u->next==t)p=p->next;
else p->next=t;
}
return s->next;
}
};