zoukankan      html  css  js  c++  java
  • 查找单向链表中倒数第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 }
  • 相关阅读:
    StateListDrawable状态选择器
    Shape
    每周随笔
    每周随笔
    每周随笔
    每周随笔
    每周随笔
    每周随笔
    每周随笔

  • 原文地址:https://www.cnblogs.com/GoAhead/p/2516024.html
Copyright © 2011-2022 走看看