码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-04-06 11:39:46      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:单链表   查找数据

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

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;

}

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

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

标签:单链表   查找数据

原文地址:http://10921020.blog.51cto.com/10911020/1760721

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!