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
4mo

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!
Select
Add answer anonymously...

Microsoft Corporation Software Developer Intern interview questions & answers

A Software Developer Intern was asked 7mo agoQ. What were your responsibilities during your internship?
A Software Developer Intern was asked 7mo agoQ. What is the difference between polymorphism and inheritance?
A Software Developer Intern was asked 9mo agoQ. Given a string, reverse the order of characters using standard data structures a...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 6mo agoQ1. What were your responsibilities during your internship?
A Software Developer Intern was asked 6mo agoQ2. What is the difference between polymorphism and inheritance?
A Software Developer Intern was asked 8mo agoQ3. Given a string, reverse the order of characters using standard data structures a...read more
Microsoft Corporation Software Developer Intern 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