My code


  • 0
    L

    class Solution {
    public:
    // 思路1:参考remove duplicates I,新链表指针直接跳过与当前节点val相等的所有节点。
    ListNode* deleteDuplicates(ListNode* head)
    {
    if(head== nullptr || head->next == nullptr)
    return head;

        ListNode fake(0);
        fake.next = head;
        ListNode *pre = &fake;
        
        while(head)
        {
            if(head->next && head->val == head->next->val)
            {
               ListNode *temp = head;
               while(temp->next && head->val == temp->next->val)
                    temp = temp->next;           // 找到重复的最后一个节点,pre跳过这些节点,到下一个节点
               head = temp;
               pre->next = head->next;
            }
            else                                //  当前节点不重复,直接加进新链表中
            {
                pre->next = head;
                pre = pre->next;
            }
            head = head->next;
        }
        return fake.next;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.