/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(head==NULL||head->next==NULL)
return head;
ListNode *dummy=new ListNode(0);
dummy->next=head;
ListNode *pre=dummy;
ListNode *cur=dummy->next;
bool needDelete=false;
while(cur!=NULL&&cur->next!=NULL)
{
if(cur->val==cur->next->val)
needDelete=true;
else
{
if(needDelete==true)
{
pre->next=cur->next;//will change the structure of the list
needDelete=false;
}
else
pre=cur;//will not change the structure of the list
}
cur=cur->next;
}
if(needDelete)
pre->next=NULL;//will change the structure of the list
return dummy->next;
}
};
【leetcode】Remove Duplicates from Sorted List II
最新推荐文章于 2023-08-30 08:07:21 发布