判斷兩鏈表是否相交

來源http://blog.csdn.net/huangxy10/article/details/8014233

只介紹O(n)算法

思路1:

將鏈表1中各結點地址存入HashTable中,

再遍歷鏈表2,如果有結點已經在HashTable中,則兩鏈表相交。


思路2:

將鏈表1的尾結點和鏈表2的首結點(注意不是頭結點)相連。

再判斷是否有環,如果有則兩鏈表相交。


思路3:(最簡單的方法)

判斷兩鏈表的尾結點是否爲同一結點,若是,則相交。

 

實現部分爲思路2。

  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. struct node * check_loop( struct node * phead )  
  66. {  
  67.     if(!phead)  
  68.          return NULL;  
  69.     struct node * pFast = phead;  
  70.     struct node * pSlow = phead;  
  71.   
  72.     int step = 1;  
  73.     while( pFast )  
  74.     {  
  75.         pFast = pFast->next;  
  76.         if( step++%2==0 )  
  77.         {  
  78.             pSlow = pSlow->next;  
  79.             if( pSlow == pFast )  
  80.                 return pSlow;  
  81.         }  
  82.     }  
  83.     return NULL;  
  84. }  
  85.   
  86. //求兩個節點的距離,即中間有多少個連線  
  87. //返回-1爲出錯,如果是同一節點,則返回0  
  88. int find_length( struct node* pNode1, struct node* pNode2 )  
  89. {  
  90.     if(!pNode1||!pNode2) return -1;  
  91.   
  92.     int len=0;  
  93.     while( pNode1 )  
  94.     {  
  95.         if( pNode1 == pNode2 )  
  96.             return len;  
  97.         pNode1 = pNode1->next;  
  98.         len++;  
  99.     }  
  100.     return -1;  
  101. }  
  102.   
  103. //找環的起始點,pTail爲快慢指針共同指向的節點  
  104. struct node * loop_first_node( struct node * phead, struct node * pTail )  
  105. {  
  106.     if( !phead || !pTail ) return NULL;  
  107.     struct node * pNode1 = pTail->next;  
  108.     struct node * pNode2 = phead->next;  
  109.     int M = find_length( pNode1, pTail );  
  110.     int N = find_length( pNode2, pTail );  
  111.   
  112.     if( M > N )  
  113.     {  
  114.         int step = M-N;  
  115.         while(step--)  
  116.             pNode1 = pNode1->next;  
  117.     }  
  118.     if( N > M )  
  119.     {  
  120.         int step = N-M;  
  121.         while(step--)  
  122.             pNode2 = pNode2->next;  
  123.     }  
  124.     while(pNode1&&pNode2)  
  125.     {  
  126.         if(pNode1 == pNode2 )  
  127.             return pNode1;  
  128.         pNode1 = pNode1->next;  
  129.         pNode2 = pNode2->next;  
  130.     }  
  131.     return NULL;  
  132. }  
  133.   
  134. void test()  
  135. {  
  136.     string str;  
  137.     cin >> str;  
  138.     struct node *phead1 = create( str );  
  139.       
  140.     int index;  
  141.     cin >> index;  
  142.     struct node * pNode1 = find_node( phead1, index );  
  143.   
  144.     cin >> str;  
  145.     struct node *phead2 = create( str );  
  146.   
  147.     struct node * pNode = phead2;  
  148.     while( pNode->next )  
  149.         pNode = pNode->next;  
  150.     pNode->next = pNode1;        //生成相交鏈表, 註釋掉之一行則爲不相交  
  151.   
  152.     while( pNode->next )           //找到鏈表1的尾結點  
  153.         pNode = pNode->next;  
  154.     pNode->next = phead2->next;    //連接鏈表2的首結點  
  155.     struct node * pTail = check_loop( phead1 ); //檢查是否有環  
  156.     struct node * pFirstLoopNode = NULL;  
  157.     if( pTail )  
  158.     {     
  159.         cout <<"cross." <<endl;  
  160.     }  
  161.     else  
  162.         cout << "no cross." <<endl;  
  163.   
  164. }  
  165.   
  166. int _tmain(int argc, _TCHAR* argv[])  
  167. {  
  168.     test();  
  169.     return 0;  
  170. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章