791. Remove bad elements in Linked List

Given the head of a Linked List and an integer val, remove all nodes having value val and return the head of the modified list.

Example 1:

Input: head -> 3 -> 4 -> 1 -> 3 -> 5 -> 7, val = 3

Output: head -> 4 -> 1 -> 5 -> 7

Explanation: The nodes with value 3 were removed.

Example 2:

Input: head -> 1 -> 2 -> 4 -> 5 -> 3 -> 7, val = 7

Output: head -> 1 -> 2 -> 4 -> 5 -> 3

Explanation: The node with value 7 was removed.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= Number of nodes in the Linked List <= 105
  • -104 <= ListNode.val <= 104
  • -104 <= val <= 104
0
/*
Definition of singly linked list:
struct ListNode
{
int val;
ListNode *next;
ListNode()
{
val = 0;
next = NULL;
}
ListNode(int data1)
{
val = data1;
next = NULL;
}
ListNode(int data1, ListNode *next1)
{
val = data1;
next = next1;
}
};
*/
 
 
class Solution {
public:
ListNode* removeBadElements(ListNode* head, int val) {
 
}
};
Test Case

Input:

Nums
Val