您的位置:首页 > 其它

【leetcode】Remove Duplicates from Sorted List

2013-09-21 20:58 375 查看
/**
* 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)
{
pre->next=cur;//dif from [Remove Duplicates from Sorted List II]
pre=cur;//dif from [Remove Duplicates from Sorted List II]
needDelete=false;
}
else
{
pre=cur;
}
}
cur=cur->next;
}
if(needDelete)
pre->next=cur;//dif from [Remove Duplicates from Sorted List II]
return dummy->next;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: