Find the k-th Node from the End of a Linked List

Given the head node of a singly linked list and an integer 'k', this problem requires you to determine the value at the k-th node from the end of the linked list.

Example:

For a given linked list image, if k=2, the value at the second node from the end is 12.

Input:

The first line of the input contains an integer T representing the number of test cases.
For each test case, the first line contains integers denoting the nodes of the linked list, ending with -1 to signify the end of the list.
The second line contains an integer k.

Output:

For each test case, return a pointer to the node at the k-th position from the end of the linked list.

Constraints:

  • 1 <= T <= 50
  • 0 <= N <= 10^5
  • -10^9 <= data <= 10^9
  • 1 <= k <= N
  • node->data ≠ -1
Note:
1. You don't need to handle the input. It's managed already. Simply implement the function to return a pointer to the k-th node from the end of the list.
2. It is guaranteed that k <= the size of the linked list.
AnswerBot
4mo

To find the k-th node from the end of a linked list, iterate through the list to determine the size, then traverse again to reach the k-th node from the end.

  • Iterate through the linked list to determin...read more

Help your peers!
Select
Add answer anonymously...

Top Software Engineer Interview Questions Asked at Uber

Q. Describe the architecture of Netflix.
Q. Given a read-only array, find the kth smallest element in an unordered array wit...read more
Q. Given a 2D matrix with some doors (D) and walls (W), how would you fill a distan...read more
Software Engineer 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