Java solution: no recursion, 1 ms


  • 0
    Z
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        } 
        ListNode patrol = head;
        while (patrol.next != null) {
            if (patrol.val == patrol.next.val) {
                patrol.next = patrol.next.next;
            } else {
                patrol = patrol.next;
            }
        }
        return head;
    }
    

    This solution uses iterative approach.


Log in to reply
 

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