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

// 查找链表的倒数第K个结点ide

示意图:spa

wKiom1cIpvrTNh1hAABdYE4x9hg680.png


PSListNode FindLastKNode(PSListNode pHead, int K )blog

{get

                 PSListNode pFast = pHead ;it

                 PSListNode pSlow = pHead ;io

                 if (pHead == NULL || K <= 0)ast

                {class

                                 return NULL ;List

                }im

                 while (--K )

                {

                                 if (pFast == NULL )

                                {

                                                 return NULL ;

                                }

                                pFast = pFast->pNext;

                }

                 while (pFast->pNext)

                {

                                pSlow = pSlow->pNext;

                                pFast = pFast->pNext;

                }

                 return pSlow;

}

建议:若是理解不清楚,必定要画个图,看着图写代码会容易不少,思路也会清晰

相关文章
相关标签/搜索