Delete Kth Node from End of Linked List

This problem requires you to delete the Kth node from the end of a given singly linked list with N nodes containing integer data.

You are asked to efficiently remove this node.

Input:

The first integer 'T' indicates the number of test cases. For each test case, the first integer is 'K', the position (1-based index) of the node from the end to delete. The following line contains space-separated integers representing the linked list elements, ending with -1 which is not an element of the list.

Output:

For each test case, output the modified linked list, separating data with a space and ending with -1.

Example:

Input:
The given linked list is 1 -> 2 -> 3 -> 4-> 'NULL' and 'K' is 2
Output:
1 -> 2 -> 4 -> 'NULL'
Explanation:

After removing the second node from the end, the linked list transforms to 1->2->4->'NULL'.

Constraints:

  • 1 <= 'T' <= 5
  • 0 <= 'N' <= 105
  • 0 <= 'K' <= 'N'
  • 1 <= 'DATA' <= 109 and 'DATA' != -1

Note: You are not required to print anything; it has already been taken care of. Just implement the function. Can you solve this without finding the length of the linked list and using O(1) extra space?

AnswerBot
7d

To delete the Kth node from the end of a singly linked list efficiently without finding the length of the list and using O(1) extra space.

  • Use two pointers approach - one pointer moves K steps ahead of...read more

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