剑指offer 面试题15 找到单链表倒数第k个结点

来源:互联网 发布:gif制作软件中文版 编辑:程序博客网 时间:2024/06/06 02:24


struct ListNode{int data;ListNode *next;}; ListNode* KthToTail(ListNode *pHead,int k){if(pHead==NULL||k<=0) return NULL;ListNode *pAhead=pHead;ListNode *pBehind=pHead;for(int i=0;i<k-1;++i){if(pAhead->next!=NULL){pAhead=pAhead->next;}else return NULL;}while(pAhead->next){pAhead=pAhead->next;pBehind=pBehind->next;}return pBehind;}


0 0