首页 > 其他 > 详细

求单链表是否带环,和环长

时间:2015-08-30 01:11:58      阅读:240      评论:0      收藏:0      [点我收藏+]

//带环返回1

//不带环返回0

int IsCycle(PLinkList pList, PLinkList*ppMeetNode)

{

Node*fast = pList;

Node*slow = pList;

while (fast&&fast->next)

{

slow = slow->next;

fast = fast->next->next;

if (slow == fast)

{

*ppMeetNode = slow;

return 1;

}

}

*ppMeetNode = NULL;

return 0;


}

int GetCycleLength(PLinkList pMeetNode)

{

assert(pMeetNode);

Node*begin = pMeetNode;

int count = 1;

assert(pMeetNode);

while (begin)

{

if (begin->next == pMeetNode)

{

return count;

}

begin = begin->next;

++count;

}

return 0;

}


求单链表是否带环,和环长

原文:http://10622551.blog.51cto.com/10612551/1689605

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!