Java Remove Duplicates from Sorted List solution


  • 0
    V

    public class Solution {

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

    }


Log in to reply
 

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