Sharing my 12ms simple C++ solution


  • 0
    T
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
     
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode* p = head;
            while(p)
            {
                if(p->next && p->next->val == p->val)
                    p->next = p->next->next;
                else
                    p = p->next;
            }
            
            return head;
        }
    };

Log in to reply
 

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