Remove Nth Node from End of Linked List

You are given a singly linked list with 'N' nodes, each containing integer data, and an integer 'K'. Your goal is to remove the 'K'th node counting from the end of this linked list.

Input:

The first line contains an integer 'T', representing the number of test cases to be processed. Each test case contains: 
1. An integer 'K', the 1-based index from the end of the node to remove.
2. A sequence of integers representing the elements of the linked list, ending with -1 (which should not be part of the list).

Output:

For each test case, output a single line containing the updated linked list in a linear form, with each element separated by a space, and end the list with -1.

Example:

Input:
1
2
1 2 3 4 -1
Output:
1 2 4 -1

Constraints:

  • 1 <= T <= 5
  • 0 <= N <= 10^5
  • 0 <= K <= N
  • 1 <= DATA <= 10^9 and DATA ≠ -1
  • Time Limit: 1 second

Note:

The function is provided for you to implement; no need to print anything as output handling is already taken care of.
AnswerBot
4mo

Remove the Kth node from the end of a singly linked list.

  • Use two pointers approach to find the Kth node from the end.

  • Handle edge cases like removing the head node or removing the last node.

  • Update the ...read more

Help your peers!
Select
Add answer anonymously...

Popular interview questions of Software Developer

A Software Developer was asked Q1. Reverse a Stack Using Recursion You are given a stack of integers. Your task is ...read more
A Software Developer was asked Q2. Remove Nth Node from End of Linked List You are given a singly linked list with ...read more
A Software Developer was asked Q3. Reverse Words in a String: Problem Statement You are given a string of length N....read more
24/7 Customer Software Developer 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