AcWing 29. 删除链表中重复的节点
原题链接
中等
作者:
warninging
,
2023-01-08 21:13:25
,
所有人可见
,
阅读 137
C++ 代码
/**
* 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 a= new ListNode(-1);
auto b=a;
while(head)
{
int t=head -> val;
bool flag=false;
while(head->next&& head->next->val==t)head=head->next,flag=true;
if(!flag)b=b->next=head;
head=head->next;
}
b->next=nullptr;
return a->next;
}
};