Friday, October 21, 2011 No. 10 - K-th Node from End Problem: Get the Kth node from end of a linked list. It counts from 1 here, so the 1st node from end is the tail of list. For instance, given a linked list with 6 nodes, whose value are 1, 2, 3, 4, 5, 6, its 3rd node from end is the node with value 4. A node in the list is defined as: struct ListNode }; Analysis: In a list with n nodes, its kth node from end should be the (n-k+1)th node from its head. Therefore, if we know the number of nodes n in a list, we can get the required node with n-k+1 steps from its head. How to get the number n?
Read full article from Coding Interview Questions: No. 10 - K-th Node from End
No comments:
Post a Comment