首页 > 代码库 > problem-whether two headless linked lists cross
problem-whether two headless linked lists cross
whether two headless linked lists cross
个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客:google搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;
博客内容:在无头节点的链表里删除元素;
博客时间:2014-4-15;
编程语言:C++ ;
编程坏境:Windows 7 专业版 x64;
编程工具:vs2008 32位编译器;
制图工具:office 2010 ppt;
硬件信息:7G-3 笔记本;
my worlds
keeping healthy is so important.
problem
solutioncheck two headless linked lists cross.
situation one: these lists don‘t have a cycle
situation two: these lists have cyclescheck they whether have ten same end.
if( _Get_end(list1) != _Get_end(list2) ) cout<<"the two list don‘t have the same end"<<endl; else cout<<"the two list have the same end"<<endl;
i will cut off one list, then check whether another is cut off. if another is cut off, then they cross, or not.sub-problem: whether a linked list have a cycle.
just make two legs have different speeds, if they meet, the list have a cycle, or not.
my experiments
situation one:
situation two:
for(int i=1;i<10;i++) { p2->next = new node(); p2 = p2->next; p2->data = http://www.mamicode.com/i;>
result: cross
my code
test.cpp
#include<iostream> #include<string> using namespace std; class node { public: int data; node * next; node(){ data = http://www.mamicode.com/0;>