查找单向链表中倒数第K个节点

基本思路:

(1)将pSlow和pFast同时指向链表的头部

(2)将快指针向后移动K位

(3)快慢指针同时移动,当pFast为空时,pSlow就指向倒数第K个节点

(4)结束

算法:

 1 LinkList* FindK(LinkList* LK,int K){
 2     LinkList *pSlow,*pFast;
 3     pSlow=pFast=LK;
 4     int i=0;
 5     while(i<K){
 6         pFast=pFast->next;
 7         i++;
 8     }
 9     while(pFast){
10         pSlow=pSlow->next;
11         pFast=pFast->next;
12     }
13     return pSlow;
14 }
原文地址:https://www.cnblogs.com/GoAhead/p/2516024.html