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
4mo

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!
Select
Add answer anonymously...

Popular interview questions of Software Engineer

A Software Engineer was asked Q1. Triplets with Given Sum Problem Given an array or list ARR consisting of N integ...read more
A Software Engineer was asked Q2. Remove Nodes with Specific Value from Linked List You are provided with a singly...read more
A Software Engineer was asked Q3. Sort Linked List Based on Actual Values Given a Singly Linked List of integers t...read more
Freshworks Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits