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.
Be the first one to answer
Add answer anonymously...
24/7 Customer 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