Simple java solution


  • 1
    S
    public ListNode removeElements(ListNode head, int val) {
    		if (head == null) {
    			return null;
    		}
    		ListNode temp = head;
    		while (temp.next != null) {
    			if (temp.next.val == val) {
    				temp.next = temp.next.next;
    			} else {
    				temp = temp.next;
    			}
    		}
    		if (head.val == val) {
    			head = head.next;
    		}
    		return head;
    	}

Log in to reply
 

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