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

示意图:

wKiom1cIpvrTNh1hAABdYE4x9hg680.png


PSListNode FindLastKNode(PSListNode pHead, int K )

{

                 PSListNode pFast = pHead ;

                 PSListNode pSlow = pHead ;

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

                {

                                 return NULL ;

                }

                 while (--K )

                {

                                 if (pFast == NULL )

                                {

                                                 return NULL ;

                                }

                                pFast = pFast->pNext;

                }

                 while (pFast->pNext)

                {

                                pSlow = pSlow->pNext;

                                pFast = pFast->pNext;

                }

                 return pSlow;

}

建议:如果理解不清楚,一定要画个图,看着图写代码会容易很多,思路也会清晰