remove a number from linked list


  • 0
    K

    '''
    class Solution {
    public ListNode removeElements(ListNode head, int val) {
    ListNode answerNode= new ListNode(0);
    answerNode.next=head;
    ListNode newNode=answerNode;
    while(newNode.next!=null){
    if(newNode.next.val==val){
    ListNode next= newNode.next;
    newNode.next=next.next;
    }
    else newNode=newNode.next;
    }
    return answerNode.next;
    }
    }
    '''


Log in to reply
 

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