Simple Java solution (1 ms, no extra space, O(n) time)


  • 0
    E
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            ListNode dummy = new ListNode(Integer.MIN_VALUE);
            dummy.next = head;
            ListNode curr = head, prev = dummy;
        
            while(curr != null){
                if(prev.val == curr.val) prev.next = curr.next; //Duplicate found
                else prev = curr;
                curr = curr.next;
            }
            return dummy.next;
        }
    }

Log in to reply
 

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