Kth element from the last in linked list

I am getting wrong answer in one of the test cases.
Approach that i used was, i took two pointers fast and slow. I move fast k steps first and then i moved both slow and fast simultaneously by one step. As fast or fast->next reaches null, slow will point to the Kth element from the last.

Hi Kishan,
Your approach seems correct. You may have missed some corner case.
Please share your code using ide.codingblocks.com .

Updated Code - https://ide.codingblocks.com/s/97807

1 Like