leetcode 82 Remove Duplicates from Sorted List II

Posted liuqiujie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 82 Remove Duplicates from Sorted List II相关的知识,希望对你有一定的参考价值。

 

/**
 * 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) {
        if(!head||!head->next) return head;
        ListNode ln(0);ln.next=head;
        ListNode* node=head,*pre=&ln,*cur=node->next;
        while(cur) {
            if(cur->val==node->val) {
                 while(cur&&cur->val==node->val) {
                    ListNode* tmp=cur;
                    cur=cur->next;
                    delete tmp;
                }//如果每次都修改node->next指针的话,更费时;
                pre->next=cur;
                delete node;
                node=pre->next;
                if(node) cur=node->next;
            }//if
           else {
               pre=node;
               node=cur;
               cur=cur->next;
           }
        }
        return ln.next;
    }
};

 

以上是关于leetcode 82 Remove Duplicates from Sorted List II的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 82 Remove Duplicates from Sorted List II

LeetCode82 Remove Duplicates from Sorted List II

[LeetCode]82. Remove Duplicates from Sorted List II

[LeetCode] 82. Remove Duplicates from Sorted List II

leetcode:82. Remove Duplicates from Sorted List II(Java)解答

一天一道LeetCode#82. Remove Duplicates from Sorted List II