How to think of a solution of this question if we have to solve it without using the length of a LinkedList???
K-th Element from last in a linked List
Use the two pointers approach:
- Maintain two pointers – reference pointer and main pointer.
- Initialize both reference and main pointers to head.
- First move reference pointer to n nodes from head.
- Now move both pointers one by one until reference pointer reaches end.
- Now main pointer will point to nth node from the end.
- Return main pointer.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.