This can be done in single traversal using two pointers . First move to the kth node from first and point it using temp pointer . Point the head node using another pointer called kthnodefromlast .Now Move the temp pointer and kthnodefromlast to next node till temp node reaches the end of the linked list.
Example:
1->2->3->4->5->6->7->NULL
For the above linked list kth(k=3) node from last is 5.
Code:(in C)
Time Complexity : O(n)
Space Complexity: O(1)
Please let me know if you have any questions .
1 comment:
I think,there is no other better solution than this..
If u know let others to know
Post a Comment