My java solution


  • 0
    C
     public class Solution {
     public ListNode removeElements(ListNode head, int val) {
       if(head == null)
    	   return null;
        
       ListNode tmp = new ListNode(0);
       tmp.next = head;
       
       ListNode previous;
       ListNode current;
       
       previous=tmp;
       current=head;
       
       while(current != null)
       {
           if(current.val == val)
           {
        	   current=current.next;
        	   previous.next=current;
           }
       
           else if(current.val != val)
           {
        	   previous=current;
        	   current=current.next;
           }
       }
       return tmp.next;
    }
    

    }


Log in to reply
 

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