A Simple C++ Code using a single pointer. Understand why this works. Simple Trick


  • 0
    C

    class Solution {
    public:
    ListNode *deleteDuplicates(ListNode *head) {

        if(!head)
            return head;
        
        ListNode *temp=head;     
        while(temp->next)
        {
            if(temp->val==temp->next->val)
                temp->next=temp->next->next;
            else
                temp=temp->next;
        }
        return head;      
    

    }
    };


  • 2
    E

    you did not delete the unnecessary memory.


Log in to reply
 

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