两个链表相交,计算相交点
来源:互联网 发布:nasa气象数据下载 编辑:程序博客网 时间:2024/05/21 05:20
来源http://blog.csdn.net/huangxy10/article/details/8014276
思路在8中有介绍
分别计算两链表的长度,为M,N。让指向长链表的指针先走,让两链表等长,
再两指针一起走,第一次相遇点即为相交点。
如果把相交链表变成一个环,则环的第一个结点即为相交点。
- // LinkTable.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- using namespace std;
- //链表的结构体
- struct node
- {
- char val;
- node * next;
- };
- //建立链表
- struct node * create( string & str_link )
- {
- int len = str_link.length();
- struct node * phead = new node(); //带有表头的链表,表头中不存储任何元素
- struct node * preNode = phead;
- for( int i=0; i<len; i++ )
- {
- struct node * pNode = new node();
- pNode->val = str_link[i];
- pNode->next = NULL;
- preNode->next = pNode;
- preNode = pNode;
- }
- return phead;
- }
- //输出链表
- void out_link( struct node * phead )
- {
- if( phead == NULL )
- return;
- struct node * pNode = phead->next;
- while( pNode )
- {
- cout <<pNode->val;
- pNode = pNode->next;
- }
- cout << endl;
- }
- //找到第index个元素
- struct node * find_node(struct node* phead, int index )
- {
- if(!phead) return NULL;
- struct node * pNode = phead;
- while( index--)
- {
- pNode = pNode->next;
- if( !pNode )
- return NULL;
- }
- return pNode;
- }
- //检查链表有无环
- //有,则返回快慢指针共同指向的点
- //无,则返回空指针
- struct node * check_loop( struct node * phead )
- {
- if(!phead)
- return NULL;
- struct node * pFast = phead;
- struct node * pSlow = phead;
- int step = 1;
- while( pFast )
- {
- pFast = pFast->next;
- if( step++%2==0 )
- {
- pSlow = pSlow->next;
- if( pSlow == pFast )
- return pSlow;
- }
- }
- return NULL;
- }
- //求两个节点的距离,即中间有多少个连线
- //返回-1为出错,如果是同一节点,则返回0
- int find_length( struct node* pNode1, struct node* pNode2 )
- {
- if(!pNode1||!pNode2) return -1;
- int len=0;
- while( pNode1 )
- {
- if( pNode1 == pNode2 )
- return len;
- pNode1 = pNode1->next;
- len++;
- }
- return -1;
- }
- //找环的起始点,pTail为快慢指针共同指向的节点
- struct node * loop_first_node( struct node * phead, struct node * pTail )
- {
- if( !phead || !pTail ) return NULL;
- struct node * pNode1 = pTail->next;
- struct node * pNode2 = phead->next;
- int M = find_length( pNode1, pTail );
- int N = find_length( pNode2, pTail );
- if( M > N )
- {
- int step = M-N;
- while(step--)
- pNode1 = pNode1->next;
- }
- if( N > M )
- {
- int step = N-M;
- while(step--)
- pNode2 = pNode2->next;
- }
- while(pNode1&&pNode2)
- {
- if(pNode1 == pNode2 )
- return pNode1;
- pNode1 = pNode1->next;
- pNode2 = pNode2->next;
- }
- return NULL;
- }
- void test()
- {
- string str;
- cout << "Input the first link:"<<endl;
- cin >> str;
- struct node *phead1 = create( str );
- int index;
- cout << "Input the index of cross node: " <<endl;
- cin >> index;
- struct node * pNode1 = find_node( phead1, index );
- cout << "Input the second link:"<<endl;
- cin >> str;
- struct node *phead2 = create( str );
- struct node * pNode = phead2;
- while( pNode->next )
- pNode = pNode->next;
- pNode->next = pNode1; //生成相交链表, 注释掉这一行则为不相交
- while( pNode->next ) //找到链表1的尾结点
- pNode = pNode->next;
- pNode->next = phead2->next; //连接链表2的首结点
- struct node * pTail = check_loop( phead1 ); //检查是否有环
- struct node * pFirstLoopNode = NULL;
- if( pTail )
- {
- cout <<"cross." <<endl;
- pFirstLoopNode = loop_first_node(phead1, pTail);
- cout <<"The cross node is: "<< pFirstLoopNode->val << endl;
- pNode->next = NULL; //还原,把环拆开
- }
- else
- cout << "no cross." <<endl;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- test();
- return 0;
- }
0 0
- 两个链表相交,计算相交点
- 10.两个单链表相交,计算相交点
- 判断两个带环链表是否相交,并求出第一个相交点
- 2.2 链表-判断两个链表是否相交并判断相交点
- 如何判断两个链表相交及找到第一个相交点
- 如何判断两个链表相交及找到第一个相交点
- 如何判断两个链表相交及找到第一个相交点
- 如何判断两个链表相交及找到第一个相交点
- 如何判断两个链表相交及找到第一个相交点
- 如何判断两个链表相交及找到第一个相交点
- 如何判断两个链表是否相交并求出相交点
- 判断两个不带环链表是否相交?若相交,求入口点。
- 如何判断两个链表相交及找到第一个相交点
- 10两链表相交,计算相交点
- 两个链表是否相交
- 两个链表是否相交
- 求两个相交链表相交的第一个元素
- 判断两个链表是否相交,若相交,求交点。
- UVA458编码水题
- hdu4787 GRE Words Revenge,AC自动机,平方分割
- Android如何利用代码动态改变字体颜色
- Java设计模式菜鸟系列(三)装饰者模式建模与实现
- hdu1753(高精度·····JAVA版)
- 两个链表相交,计算相交点
- bzoj1689 [Usaco2005 Open] Muddy roads 泥泞的路
- 章鱼哥出品_VB.NET ComboBox、 TextBox 实现自动完成、自动过滤 模糊查询的功能
- POJ 3046 - Ant Counting(dp多种背包变型)
- 程序员哲思-“不知道”的三重境界
- .net强制退出主窗口的方法——Application.Exit()方法和Environment.Exit(0)方法
- POJ 3344 & HDU 2414 Chessboard Dance(模拟)
- NSSortDescriptor(数组排序)
- 判断两链表是否相交