Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
- Implementation
class Solution{
public:
ListNode *removeElements(ListNode *head, int val){
ListNode *curr = head;
ListNode *prev = NULL;
while (curr != NULL){
if (curr->val == val){
if (prev == NULL)
head = curr->next;
else
prev->next = curr->next;
}
else{
prev = curr;
}
curr = curr->next;
}
return head;
}
};
- if head is null return null.
- if value of current node is val we not include it else we include it.
class Solution{
public:
ListNode *removeElements(ListNode *head, int val){
if (head == NULL) return head;
head->next = removeElements(head->next, val);
return head->val == val ? head->next : head;
}
};