What's wrong with my code in java?


  • 0
    J
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if(head==null) return null;
            ListNode i=head;
            ListNode iNext=head.next;
        
            while(iNext!=null){
                if(i.val==iNext.val){
                    iNext=iNext.next;
                    i=iNext;
                }
                else{
                    i=iNext;
                }
    
            }
            return i;
    
        }
    }

  • 0
    M

    When you do

    iNext=iNext.next;
    i=iNext;
    

    you are just advancing. You would need to change the value of i.next to skip iNext. Also i should be assigned to iNext before you change iNext, otherwise iNext and i are the same.


  • 0

    I think if i.val != iNext.val someTimes,i is iNext all the time


Log in to reply
 

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