Input: [1,2] 2 Output: [1,2] why wrong answer? What's [1,2]?Can we have two heads in the same list?


  • 0
    J

    public class Solution {

    public ListNode removeElements(ListNode head, int val) {
        ListNode current = head;
        while(!(current == null)){
            if(head.val == val){
                head = head.next;
                current = head;
            }
            else if(current.val == val && current.next != null){
                current.val = current.next.val;
                current.next = current.next.next;
            }
            else if(current.val == val && current.next == null){
                current = null;
            }
            else{
                current = current.next;
            }
        }
        return head;
    }
    

    }


Log in to reply
 

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