My Easy to Understand Java Solution


  • 0
    X
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if(head==null || head.next==null)   return head;
            ListNode start = new ListNode(0);
            start.next = head;
            ListNode pre = start;
            boolean dup = false;
            while(head.next != null){
                if(head.val == head.next.val){
                    head.next = head.next.next;
                    dup = true;
                }
                else if(dup){
                    head = head.next;
                    pre.next = head;
                    dup = false;
                }
                else{
                    pre = head;
                    head = head.next;
                }
            }
            if(dup) pre.next = head.next;
            return start.next;
        }
    }

Log in to reply
 

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