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

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

时间:2015-08-30 01:13:00      阅读:153      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!