判断单链表是否有环,以及如何找起始点,环的长度

13 篇文章 0 订阅

来源http://blog.csdn.net/huangxy10/article/details/8014148

判断单链表是否有环?

思路:

快慢指针,快指针每次走两步,慢指针每次走一步。

每次判断快指针是否到头了以及快慢指针是否指向同一元素。

快指针走到头了,则没有环;

如果快指针和慢指针指向同一个元素,则有环。

 

如何找到环的起始点?

思路:

如果有环,则快慢指针一定相遇在环上。

将环从快慢指针相遇结点剪开,则变成了两个单链表相交的问题。

设两个单链表的长度分别为N,M,用两个指针分别指向两链表第一个结点,

让位于长链表上的指针先走abs(N-M)步。然后一起走,相遇时则为环的起始点。


如何知道环的长度?

绕一圈即可。

  1. // LinkTable.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include <string>  
  7. using namespace std;  
  8.   
  9. //链表的结构体  
  10. struct node  
  11. {  
  12.     char val;  
  13.     node * next;  
  14. };  
  15.   
  16. //建立链表  
  17. struct node * create( string & str_link )  
  18. {  
  19.     int len = str_link.length();  
  20.   
  21.     struct node * phead = new node();     //带有表头的链表,表头中不存储任何元素  
  22.     struct node * preNode = phead;  
  23.     forint i=0; i<len; i++ )  
  24.     {  
  25.         struct node * pNode = new node();  
  26.         pNode->val = str_link[i];  
  27.         pNode->next = NULL;  
  28.         preNode->next = pNode;  
  29.         preNode = pNode;  
  30.     }  
  31.     return phead;  
  32. }  
  33.   
  34. //输出链表  
  35. void out_link( struct node * phead )  
  36. {  
  37.     if( phead == NULL )  
  38.         return;  
  39.     struct node * pNode = phead->next;  
  40.     while( pNode )  
  41.     {  
  42.         cout <<pNode->val;  
  43.         pNode = pNode->next;  
  44.     }  
  45.     cout << endl;  
  46. }  
  47.   
  48. //找到第index个元素  
  49. struct node * find_node(struct node* phead, int index )  
  50. {  
  51.     if(!phead) return NULL;  
  52.     struct node * pNode = phead;  
  53.     while( index--)  
  54.     {  
  55.         pNode = pNode->next;  
  56.         if( !pNode )  
  57.             return NULL;  
  58.     }  
  59.     return pNode;  
  60. }  
  61.   
  62.   
  63.   
  64. //检查链表有无环  
  65. //有,则返回快慢指针共同指向的点  
  66. //无,则返回空指针  
  67. struct node * check_loop( struct node * phead )  
  68. {  
  69.     if(!phead)  
  70.          return NULL;  
  71.     struct node * pFast = phead;  
  72.     struct node * pSlow = phead;  
  73.   
  74.     int step = 1;  
  75.     while( pFast )  
  76.     {  
  77.         pFast = pFast->next;  
  78.         if( step++%2==0 )  
  79.         {  
  80.             pSlow = pSlow->next;  
  81.             if( pSlow == pFast )  
  82.                 return pSlow;  
  83.         }  
  84.     }  
  85.     return NULL;  
  86. }  
  87.   
  88. //求两个节点的距离,即中间有多少个连线  
  89. //返回-1为出错,如果是同一节点,则返回0  
  90. int find_length( struct node* pNode1, struct node* pNode2 )  
  91. {  
  92.     if(!pNode1||!pNode2) return -1;  
  93.   
  94.     int len=0;  
  95.     while( pNode1 )  
  96.     {  
  97.         if( pNode1 == pNode2 )  
  98.             return len;  
  99.         pNode1 = pNode1->next;  
  100.         len++;  
  101.     }  
  102.     return -1;  
  103. }  
  104.   
  105. //找环的起始点,pTail为快慢指针共同指向的节点  
  106. struct node * loop_first_node( struct node * phead, struct node * pTail )  
  107. {  
  108.     if( !phead || !pTail ) return NULL;  
  109.     struct node * pNode1 = pTail->next;  
  110.     struct node * pNode2 = phead->next;  
  111.     int M = find_length( pNode1, pTail );  
  112.     int N = find_length( pNode2, pTail );  
  113.   
  114.     if( M > N )  
  115.     {  
  116.         int step = M-N;  
  117.         while(step--)  
  118.             pNode1 = pNode1->next;  
  119.     }  
  120.     if( N > M )  
  121.     {  
  122.         int step = N-M;  
  123.         while(step--)  
  124.             pNode2 = pNode2->next;  
  125.     }  
  126.     while(pNode1&&pNode2)  
  127.     {  
  128.         if(pNode1 == pNode2 )  
  129.             return pNode1;  
  130.         pNode1 = pNode1->next;  
  131.         pNode2 = pNode2->next;  
  132.     }  
  133.     return NULL;  
  134. }  
  135.   
  136. void test()  
  137. {  
  138.     string str;  
  139.     cin >> str;  
  140.     struct node *phead = create( str );  
  141.       
  142.     int index;  
  143.     cin >> index;  
  144.     struct node * pNode1 = find_node( phead, index );  
  145.     struct node * pNode = phead;  
  146.     while( pNode->next )  
  147.         pNode = pNode->next;  
  148.     pNode->next = pNode1;        //生成有环链表, 注释掉之一行则为无环链表  
  149.     struct node * pTail = check_loop( phead );  
  150.     struct node * pFirstLoopNode = NULL;  
  151.     if( pTail )  
  152.     {     
  153.         cout <<"The link has loop." <<endl;  
  154.         pFirstLoopNode = loop_first_node(phead, pTail);  
  155.         cout <<"First Loop Node is: " << pFirstLoopNode->val <<endl;  
  156.         cout <<"The number of node in loop is: " << find_length( pTail->next, pTail)+1 <<endl;  
  157.     }  
  158.     else  
  159.         cout << "The link table has no loop." <<endl;  
  160.   
  161. }  
  162.   
  163. int _tmain(int argc, _TCHAR* argv[])  
  164. {  
  165.     test();  
  166.     return 0;  
  167. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值