Find the Kth to Last Element of a Singly Linked List

It is possible to a recursive solutions but I will use a simple runner logic. Recursive solutions are usually less optimal.

Note here that, in our logic K=1 would return the last element in the linked list. Similarly, K=2 would return the second last element.

The suggested solution here is to use two pointers:

Let’s have a look at the code:

As usual the code is available here:

comments powered by Disqus