leetcode 82 Remove Duplicates from Sorted List II C++

    xiaoxiao2021-04-11  33

    这道题题意很简单,但是容易出错,特别是第一个Node需要删掉的情况,所以可以使用一个dumyNode来做,简单而且不容易出错。

    ListNode* deleteDuplicates(ListNode* head) { if (!head || !head->next) return head; ListNode *dummy = new ListNode(0); dummy->next = head; head = dummy; while(head->next && head -> next -> next) { if(head->next->val == head->next->next->val) { int val = head->next->val; while(head->next && head->next->val == val) { head->next = head->next->next; } } else { head = head->next; } } return dummy->next; }

    转载请注明原文地址: https://ju.6miu.com/read-666965.html

    最新回复(0)