基本思路:

(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 }

相关文章:

  • 2022-12-23
  • 2021-07-23
  • 2021-07-27
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-22
  • 2021-05-17
  • 2021-10-04
  • 2022-12-23
  • 2021-08-03
相关资源
相似解决方案