
1. 确定两个单链列表是否相交

思想分析:

最简单,最直接的方法是遍历两个链表,以确定尾节点是否相等. 如果它们相等,则它们相交或不相交.

bool CheckCross(const List& list1, const List& list2)//list1,list2为两个对象
{
Node* l1 = list1._head;
Node* l2 = list2._head;
while (l1->_next)//找到list1的尾节点
{
l1 = l1->_next;
}
while (l2->_next)//找到list2的尾节点
{
l2 = l2->_next;
}
if (l1 == l2)
{
return true;//相交
}
return false;//不相交
}

2. 找到两个单个链表的交点
思想分析:
在两个单个链表的长度相等的情况下,这是最简单的. 它只需要同时遍历两个链接列表并进行连续比较. 如果它们相等,则它们是相交,否则它们不是相交. 但是,如果两个链表的长度不相等c/c++链表,则可以通过两个链表的长度之差遍历较长的链表c/c++链表,然后同时遍历.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-154519-1.html
怕什么