C语言——链表倒数结点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Eleanor_12/article/details/53792044

编写一个程序,实现输出链表中倒数第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;
}

猜你喜欢

转载自blog.csdn.net/Eleanor_12/article/details/53792044