My Iterative Java Solution


  • 0
    L
    public ListNode deleteDuplicates(ListNode head) {
            ListNode dummy = new ListNode(0);
            ListNode pre = dummy;
            ListNode node = head;
            while (node != null) {
                ListNode t = node;
                while (t.next != null && t.next.val == t.val)
                    t = t.next;
                if (t == node) {
                    pre.next = node;
                    pre = node;
                    node = node.next;
                    pre.next = null;//unlink
                } else
                    node = t.next;
            }
            return dummy.next;
        }
    

Log in to reply
 

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