29. 删除链表中重复的节点

题目地址:https://www.acwing.com/problem/content/27/

AC代码

/**
 * 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) {
        if(!head || !head->next) return head;
        ListNode* dummy=new ListNode(-1);
        dummy->next=head;
        ListNode* pre=dummy,*cur=head;
        while(cur){
            if(cur->next && cur->next->val == cur->val){
                while(cur->next && cur->next->val == cur->val)
                    cur=cur->next;
                pre->next=cur->next;
                cur=cur->next;
            }
            else{
                pre=cur;
                cur=cur->next;
            }
        }
        return dummy->next;
    }
};

你可能感兴趣的:(29. 删除链表中重复的节点)