Share my java answer 353ms

  • 0

    create a prehead in order to save head for return..

    public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head==null) return head;
    	ListNode prehead=new ListNode(-1);;
    	ListNode p=prehead;
    	//everytime check if should be removed..
    	//p is always being checked.
    			//move one step forward;


  • 0

    Hi. Thank you for your answer. I have a question about the return value. My solution is to try to return not, and it doesn't work. So could you please explain it a little bit?

    Thank you!

  • 0

    your will be null at the end of while loop.

Log in to reply

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