Simple C++ solution, and need help on refreshing my C++


  • 0
    Z

    Basically a 2-pointer method

    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode* dummy = new ListNode(0);
            int tmp = 0;
            
            // A *//
            // ListNode* cur = new ListNode(0); //
            // dummy->next = cur->next = head; //
            
             // B //
            ListNode* cur = dummy;
            dummy->next = head;
            
            while (cur->next and cur->next->next) {
                if (cur->next->val != cur->next->next->val)
                    cur = cur->next;
                else {
                    tmp = cur->next->val;
                    while (cur->next and cur->next->val == tmp)
                        cur->next = cur->next->next;
                }
            }
            return dummy->next;
        }
    };
    

    The questions why with code segment A it is not working, but with B it is working?

    Thanks!


Log in to reply
 

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