Java easy to understand solution, iterating through the list O(1) space


  • 0
    J
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if(head == null) return null;
            
            ListNode curr = head;
            ListNode fast = head.next;
            
            
            while(fast!=null){
                if(fast.val != curr.val){
                    curr.next = fast;
                    curr = fast;
                }
                
                fast = fast.next;
            }
            
            curr.next = null;
            return head;
        }
    }

Log in to reply
 

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