程序题——链表倒数结点

来源:互联网 发布:简单的加密 java 编辑:程序博客网 时间:2024/06/16 16:41

编写一个程序,实现输出链表中倒数第K个结点

ListNode * FindKthToTail(LisNode *head, unsigned int k){    if(head == NULL || K == 0)            //入口参数检查!!!    {        return NULL;    }    int i;    ListNode *pAhead = head;    ListNode *pBehind = NULL;    for(i = 0; i < k - 1; ++i)    {        if(pAhead->next != NULL)        {             pAhead = pAhead->next;        }        else       {           return NULL;       }    }    pBehind = head;    while(pAhead->next != NULL)    {        pAhead = pAhead->next;        pBehind = pBehind->next;    }    Return pBehind;}
0 0
原创粉丝点击