两个链表相交,计算相交点

来源:互联网 发布:nasa气象数据下载 编辑:程序博客网 时间:2024/05/21 05:20

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

思路在8中有介绍

分别计算两链表的长度,为M,N。让指向长链表的指针先走,让两链表等长,

再两指针一起走,第一次相遇点即为相交点。

如果把相交链表变成一个环,则环的第一个结点即为相交点。


[cpp] view plaincopyprint?
  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.     cout << "Input the first link:"<<endl;  
  138.     cin >> str;  
  139.     struct node *phead1 = create( str );  
  140.       
  141.     int index;  
  142.     cout << "Input the index of cross node: " <<endl;  
  143.     cin >> index;  
  144.     struct node * pNode1 = find_node( phead1, index );  
  145.     cout << "Input the second link:"<<endl;  
  146.     cin >> str;  
  147.     struct node *phead2 = create( str );  
  148.   
  149.     struct node * pNode = phead2;  
  150.     while( pNode->next )  
  151.         pNode = pNode->next;  
  152.     pNode->next = pNode1;        //生成相交链表, 注释掉这一行则为不相交  
  153.   
  154.     while( pNode->next )           //找到链表1的尾结点  
  155.         pNode = pNode->next;  
  156.     pNode->next = phead2->next;    //连接链表2的首结点  
  157.     struct node * pTail = check_loop( phead1 ); //检查是否有环  
  158.     struct node * pFirstLoopNode = NULL;  
  159.     if( pTail )  
  160.     {     
  161.         cout <<"cross." <<endl;  
  162.         pFirstLoopNode = loop_first_node(phead1, pTail);  
  163.         cout <<"The cross node is: "<< pFirstLoopNode->val << endl;  
  164.         pNode->next = NULL;            //还原,把环拆开  
  165.     }  
  166.     else  
  167.         cout << "no cross." <<endl;  
  168.   
  169. }  
  170.   
  171. int _tmain(int argc, _TCHAR* argv[])  
  172. {  
  173.     test();  
  174.     return 0;  
  175. }  

0 0
原创粉丝点击