Delete a Node in a Linked List

Given a reference to a node in a singly linked list, your task is to delete that node. Every node in the list has a unique value. The head of the linked list will not be provided.

Input:
The first line contains an integer 'T' indicating the number of test cases. Each test case consists of two lines: The first line contains space-separated integers representing the values of the linked list nodes, ending in -1, indicating the end of the list. The second line contains a single integer K indicating the value of the node to be deleted.
Output:
For each test case, output the updated list of node values separated by a space, ending with '-1'. Each result should be on a new line.
Example:
Input:
2
4 5 1 9 -1
5
1 2 3 4 -1
3
Output:
4 1 9 -1
1 2 4 -1

Constraints:

  • 1 <= T <= 100
  • 2 <= N <= 5000
  • -109 <= NODE.DATA <= 109 and NODE.DATA != -1
Note:

No need to print anything yourself. Implement the function as requested.

AnswerBot
10d

To delete a node in a singly linked list given a reference to the node.

  • Traverse the linked list to find the node to be deleted.

  • Update the value of the node to be deleted with the value of the next nod...read more

Help your peers!
Add answer anonymously...
Paytm Software Developer 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