Remove the Kth Node from the End of a Linked List

You are given a singly Linked List with 'N' nodes containing integer data and an integer 'K'. Your task is to delete the Kth node from the end of this Linked List.

Input:

 The first line of input contains an integer 'T', the number of test cases or queries. For each test case:   - The first line contains an integer 'K', indicating the position (1-based) from the end of the list to be removed.   - The second line lists the elements of the singly linked list, separated by spaces, ending with -1. The value -1 is a terminator and not a part of the list. 

Output:

 For each test case, output a single line containing the updated linked list with elements separated by a single space, concluding the list with -1. Print each test case on a separate line. 

Example:

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

Constraints:

  • 1 <= 'T' <= 5
  • 0 <= 'N' <= 105
  • 0 <= 'K' <= 'N'
  • 1 <= 'DATA' <= 109 and 'DATA' != -1
  • Time Limit: 1 sec.

Note:

You only need to implement the function. Do not print anything as it will be handled for you.
AnswerBot
1d

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

  • Traverse the linked list to find the length 'N'.

  • Calculate the position to delete from the beginning as 'N - K + 1'.

  • Delete the node at the calcu...read more

Help your peers!
Add answer anonymously...
Amdocs Senior Java 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