Neat Java solution


  • 0
    Y
        public ListNode deleteDuplicates(ListNode head) {
            ListNode helper = new ListNode(0);
            ListNode tail = helper;
            while (head != null) {
                if (head.next == null || head.val != head.next.val) {
                    tail.next = head;
                    tail = tail.next;
                }
                while(head.next != null && head.val == head.next.val) head = head.next;
                head = head.next;
            }
            tail.next = null;
            return helper.next;
        }
    

Log in to reply
 

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