My easy solution 8ms


  • 0
    R
    struct ListNode* removeElements(struct ListNode* head, int val) {
    	if (head == NULL)
    	return NULL;
    struct ListNode* head1 = head;
    struct ListNode* head2 = head->next;
    while (head2)
    {
    	if (head2->val == val)
    	{
    		head1->next = head2->next;
    	}
    	else
    	{
    		head1 = head1->next;
    	}
    	head2 = head2->next;
    }
    if (head->val == val)
    	return head->next;
    else
    	return head;
    

    }


Log in to reply
 

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