AcWing 29. 删除链表中重复的节点
原题链接
简单
作者:
hh_88
,
2024-10-13 12:35:25
,
所有人可见
,
阅读 1
/**
* 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) {
ListNode* tou=new ListNode(0);
tou->next=head;
ListNode* d=tou;
ListNode* p=head;
while(p&&p->next)
{
if(d->next->val == p->next->val)
{
int val=d->next->val;
while(p->next&&p->next->val==val) {p=p->next;}
d->next=p->next;
p=d->next;
}
else
{
// if(p->next==NULL) return d;
d=p;
p=p->next;
}
}
return tou->next;
}
};