Java solution O(n) 1ms


  • 0
    M

    Simple solution iterating through the list and checking if the next value is the same as current one and skipping that node.

    public class Solution {
        
        public ListNode deleteDuplicates(ListNode head) {
          
          if (head == null || head.next == null) return head;
          
          ListNode n = head;
          while (n != null) {
            
            while (n.next != null && n.val == n.next.val) {
                n.next = n.next.next;
            }
            
            n = n.next;
          }
           
          return head;  
        }
        
    }
    

Log in to reply
 

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