首先想到的是链表逆序, 然后输出第K个节点.
还可以先计算链表中节点个数n, 倒数第0个, 就是顺数第n - 0 -1个.
typedef struct ListNode{
int m_nKey;
struct ListNode * m_pNext;
}ListNode;
ListNode * retKNode(ListNode * list, int k)
{int n = 0;
ListNode * current = list->m_pNext;
while(current)
{
n++;
current = current->m_pNext;
}
if(k < 0 || k >= n)
{
current = NULL;
}
else
{
int i;
current = list->m_pNext;
for(i = 0; i < n -k-1; i++)
{
current = current->m_pNext;
}
}
return current;
}