14 lines, one double pointer c++ solution


  • 0
    Q

    use one double pointer, no temp pointer

    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode **p = &head;
            while(*p) {
                if( (*p) && (*p)->next && (*p)->val == (*p)->next->val) {
                    int val = (*p)->val;
                    while((*p) && (*p)->val == val) {
                        (*p) = (*p)->next;
                    }
                }
                else {  //if (*p) could be null, must be careful.
                    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.