Find the kth to last element of singly Linked List
Problem
Implement an algorithm to find the kth to last element of singly Linked List.
How to Solve
This problem can be solved by Recursive
or normal traverse
. In recursive case, we have to create IntWrapper Class
and implement like this.
But this recursive solution takes O(n) space so that it has to memorize each value during recursive calls.
Better Solution
By traversing the Linked List like this, we can get the Node with O(1) space and O(n) time.