输入一个单向链表,输出该链表中倒数第k个结点。链表的倒数第0个结点为链表的尾指针
typedef struct _node_t
{
struct _node_t *next;
int data;
}Node;
Node *list_k_node(Node * head, int k)
{
Node *p=head, *pk=head;
if (NULL == head || (0 >= k))
{
return NULL;
}
for (; k > 0; k--) ///K > 0
{
if (pk->next!=NULL)
pk = pk->next;
else
return NULL;
}
while (pk->next!=NULL)
{
p=p->next;
pk=pk->next;
}
return p;
}