Simple recursive C++ solution


  • 0
    class Solution {
    public:
        ListNode* delDup(ListNode* l, bool dup) {
            if (!l) return l;
            if (!l->next) return dup ? NULL : l;
            bool dup1 = l->val == l->next->val;
            l->next = delDup(l->next, dup1);
            return dup || dup1 ? l->next : l;
        }
    
        ListNode* deleteDuplicates(ListNode* head) {
            return delDup(head, false);
        }
    };
    

Log in to reply
 

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