LeetCode:83. Remove Duplicates from Sorted List

转载请注明, 原文地址:LeetCode:83. Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

要点

和82一样 遍历一遍,但是pre->next移动到cur的位置,不是cur->next的位置

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head || !head->next) return head;
        ListNode* dup=new ListNode(-1);
        auto pre=dup;
        dup->next=head;
        while(pre->next){
            auto cur=pre->next;
            while(cur->next && cur->next->val == cur->val ){
                cur=cur->next;
            }
            if(cur!=pre->next)pre->next=cur;
            else pre=pre->next;
        }
        return dup->next;
    }
};

转载请注明:

转载自YuLai's Blog,原文地址:LeetCode:83. Remove Duplicates from Sorted List

发表评论

发表评论

*

沙发空缺中,还不快抢~