1ms Java solution. Clean, easy, readable.


  • 1
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode skipper = head;
            ListNode stepper = head.next;
            
            while(stepper != null) {
                if(skipper.val == stepper.val) {
                    stepper = stepper.next;
                    skipper.next = null;
                } else {
                    skipper.next = stepper;
                    skipper = stepper;
                }
            }
            return head;
        }
    }
    

Log in to reply
 

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