Remove Nodes with Specific Value from Linked List

You are provided with a singly linked list consisting of integer values and an integer 'K'. Your task is to eliminate all nodes from the linked list that have a value equal to 'K'.

A singly linked list is a linear data structure that allows traversal in one direction, from the head to the tail. Each node consists of data and a reference to the next node.

Example:

Input:
3
1 2 3 4 -1
2
4 4 4 4 -1
4
5 5 -1
5
Output:
1 3 4
EMPTY
5
Explanation:

In the first test case, node with value 2 is removed: [1, 3, 4]. In the second, all nodes are removed because all have the value 4, resulting in an empty list. In the third, no nodes have the value 5, so the list remains [5].

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^5
  • 0 <= node.data <= 10^9 and node.data != -1
  • 0 <= K <= 10^9
Note:
You are not required to print anything; the function is provided for implementation.
AnswerBot
5d

Remove nodes with specific value from a singly linked list.

  • Traverse the linked list and remove nodes with value equal to 'K'.

  • Update the references of the previous node to skip the removed node.

  • Handle ...read more

Help your peers!
Add answer anonymously...
Freshworks Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter