Remove Duplicates from Sorted List II Java solution


  • 0
    V

    public class Solution {

    public ListNode deleteDuplicates(ListNode head) {
    
        if(head == null || head.next == null){
            return head;
        }
        
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode n = dummy;
        ListNode i = head;
        ListNode j = head.next;
        
        while(i != null && j != null){
            if(i.val == j.val){
                while(i != null && j != null && i.val == j.val){
                    i = i.next;
                    j = j.next;
                }
                n.next = j;
                i = i.next;
                if(j != null) j = j.next;
            }else{
                n = n.next;
                i = i.next;
                j = j.next;
            }
        }
        
        return dummy.next;
    }
    

    }


Log in to reply
 

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