How would you go about finding the kth to last element in a singly linked list?
Respostas da entrevista
Sigiloso
5 de fev. de 2013
use 2 pointers
pointer 1 is 7 units ahead in position relative to pointer 2. Increment both
pointer 1 reach end -> return pointer 2
5
Sigiloso
11 de nov. de 2012
First check the size of the linkedList- O(1) operation
Then walk through the linkedList from the head to the nth element, use a counter to keep track of how many elements you have walked through.
return linkedListSize-number of elements you have walked through.