输出链表中倒数第K个结点

来源:互联网 发布:nginx域名重定向404 编辑:程序博客网 时间:2024/05/29 12:46

编写一个程序,实现输出链表中倒数第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
原创粉丝点击