剑指Offer面试题:31.两个链表的第一个公共节点
一、题目:两个链表的第一个公共节点
题目:输入两个链表,找出它们的第一个公共结点。
链表结点定义如下,这里使用C#语言描述:
public class Node
{
public int key;
public Node nextNode; public Node(int key)
{
this.key = key;
}
}
二、解题思路
2.1 蛮力法
碰到这道题,很多人的第一反应就是蛮力法:在第一链表上顺序遍历每个结点,每遍历到一个结点的时候,在第二个链表上顺序遍历每个结点。如果在第二个链表上有一个结点和第一个链表上的结点一样,说明两个链表在这个结点上重合,于是就找到了它们的公共结点。如果第一个链表的长度为m,第二个链表的长度为n,显然该方法的时间复杂度是O(mn)。
2.2 借助外部空间法
首先,经过分析我们发现两个有公共结点而部分重合的链表,拓扑形状看起来像一个Y,而不可能像X,如下图所示,两个链表在值为6的结点处交汇:

如果两个链表有公共结点,那么公共结点出现在两个链表的尾部。如果我们从两个链表的尾部开始往前比较,最后一个相同的结点就是我们要找的结点。But,在单链表中只能从头结点开始按顺序遍历,最后才能到达尾结点。最后到达的尾结点却要最先被比较,这是“后进先出”的特性。于是,我们可以使用栈的特点来解决这个问题:分别把两个链表的结点放入两个栈里,这样两个链表的尾结点就位于两个栈的栈顶,接下来比较两个栈顶的结点是否相同。如果相同,则把栈顶弹出接着比较下一个栈顶,直到找到最后一个相同的结点。
public static Node FindFirstCommonNode(Node head1, Node head2)
{
if(head1 == null || head2 == null)
{
return null;
} Stack<Node> stack1 = new Stack<Node>();
Stack<Node> stack2 = new Stack<Node>(); while(head1 != null)
{
stack1.Push(head1);
head1 = head1.nextNode;
} while(head2 != null)
{
stack2.Push(head2);
head2 = head2.nextNode;
} Node node1 = null;
Node node2 = null;
Node common = null; while(stack1.Count > && stack2.Count > )
{
node1 = stack1.Peek();
node2 = stack2.Peek(); if (node1.key == node2.key)
{
common = node1;
stack1.Pop();
stack2.Pop();
}
else
{
break;
}
} return common;
}
在上述思路中,我们需要用两个辅助栈。如果链表的长度分别为m和n,那么空间复杂度是O(m+n)。这种思路的时间复杂度也是O(m+n)。和最开始的蛮力法相比,时间效率得到了提高,相当于是用空间消耗换取了时间效率。
2.3 不借助外部空间法
那么,可不可以不借助栈来实现了呢?答案是可以的,我们可以首先遍历两个链表得到它们的长度,就能知道哪个链表比较长,以及长的链表比短的链表多几个结点。在第二次遍历的时候,在较长的链表上先走若干步,接着再同时在两个链表上遍历,找到的第一个相同的结点就是它们的第一个公共结点。
比如在上图的两个链表中,我们可以先遍历一次得到它们的长度分别为5和4,也就是较长的链表与较短的链表相比多一个结点。第二次先在长的链表上走1步,到达结点2。接下来分别从结点2和结点4出发同时遍历两个结点,直到找到它们第一个相同的结点6,这就是我们想要的结果。
public static Node FindFirstCommonNode(Node head1, Node head2)
{
// 得到两个链表的长度
int length1 = GetListLength(head1);
int length2 = GetListLength(head2);
int diff = length1 - length2; Node headLong = head1;
Node headShort = head2;
if (diff < )
{
headLong = head2;
headShort = head1;
diff = length2 - length1;
}
// 先在长链表上走几步
for (int i = ; i < diff; i++)
{
headLong = headLong.nextNode;
}
// 再同时在两个链表上遍历
while (headLong != null && headShort != null && headLong != headShort)
{
headLong = headLong.nextNode;
headShort = headShort.nextNode;
} Node commonNode = headLong;
return commonNode;
} private static int GetListLength(Node head)
{
int length = ;
Node tempNode = head;
while (tempNode != null)
{
tempNode = tempNode.nextNode;
length++;
} return length;
}
上述思路与借助栈的方法的时间复杂度都是O(m+n),但我们不再需要辅助的栈,因此提高了空间效率。
三、单元测试
3.1 测试用例:功能测试与特殊输入测试
[TestClass]
public class CommonNodeHelperTest
{
private void DestoryNode(Node node)
{
if (node != null)
{
node = null;
}
} // 第一个公共结点在链表中间
// 1 - 2 - 3 \
// 6 - 7
// 4 - 5 /
[TestMethod]
public void FindTest1()
{
Node node1 = new Node();
Node node2 = new Node();
Node node3 = new Node();
Node node4 = new Node();
Node node5 = new Node();
Node node6 = new Node();
Node node7 = new Node(); // first
node1.nextNode = node2;
node2.nextNode = node3;
node3.nextNode = node6;
node6.nextNode = node7;
// second
node4.nextNode = node5;
node5.nextNode = node6; Node actual = CommonNodeHelper.FindFirstCommonNode(node1, node4);
Assert.AreEqual(actual.key, ); DestoryNode(node1);
DestoryNode(node2);
DestoryNode(node3);
DestoryNode(node4);
DestoryNode(node5);
DestoryNode(node6);
DestoryNode(node7);
} // 没有公共结点
// 1 - 2 - 3 - 4
//
// 5 - 6 - 7
[TestMethod]
public void FindTest2()
{
Node node1 = new Node();
Node node2 = new Node();
Node node3 = new Node();
Node node4 = new Node();
Node node5 = new Node();
Node node6 = new Node();
Node node7 = new Node(); // first
node1.nextNode = node2;
node2.nextNode = node3;
node3.nextNode = node4; // second
node5.nextNode = node6;
node6.nextNode = node7; Node actual = CommonNodeHelper.FindFirstCommonNode(node1, node5);
Assert.AreEqual(actual, null); DestoryNode(node1);
DestoryNode(node2);
DestoryNode(node3);
DestoryNode(node4);
DestoryNode(node5);
DestoryNode(node6);
DestoryNode(node7);
} // 公共结点是最后一个结点
// 5 - 6 \
// 7
// 1 - 2 - 3 - 4 /
[TestMethod]
public void FindTest3()
{
Node node1 = new Node();
Node node2 = new Node();
Node node3 = new Node();
Node node4 = new Node();
Node node5 = new Node();
Node node6 = new Node();
Node node7 = new Node(); // first
node1.nextNode = node2;
node2.nextNode = node3;
node3.nextNode = node4;
node4.nextNode = node7;
// second
node5.nextNode = node6;
node6.nextNode = node7; Node actual = CommonNodeHelper.FindFirstCommonNode(node5, node1);
Assert.AreEqual(actual.key, ); DestoryNode(node1);
DestoryNode(node2);
DestoryNode(node3);
DestoryNode(node4);
DestoryNode(node5);
DestoryNode(node6);
DestoryNode(node7);
} // 公共结点是第一个结点
// 1 - 2 - 3 - 4 - 5
// 两个链表完全重合
[TestMethod]
public void FindTest4()
{
Node node1 = new Node();
Node node2 = new Node();
Node node3 = new Node();
Node node4 = new Node();
Node node5 = new Node();
Node node6 = new Node();
Node node7 = new Node(); // first & second
node1.nextNode = node2;
node2.nextNode = node3;
node3.nextNode = node4;
node4.nextNode = node5; Node actual = CommonNodeHelper.FindFirstCommonNode(node1, node1);
Assert.AreEqual(actual.key, ); DestoryNode(node1);
DestoryNode(node2);
DestoryNode(node3);
DestoryNode(node4);
DestoryNode(node5);
DestoryNode(node6);
DestoryNode(node7);
} // 输入的两个链表有一个空链表
[TestMethod]
public void FindTest5()
{
Node node1 = new Node();
Node node2 = new Node();
Node node3 = new Node();
Node node4 = new Node();
Node node5 = new Node(); // first & second
node1.nextNode = node2;
node2.nextNode = node3;
node3.nextNode = node4;
node4.nextNode = node5; Node actual = CommonNodeHelper.FindFirstCommonNode(node1, null);
Assert.AreEqual(actual, null); DestoryNode(node1);
DestoryNode(node2);
DestoryNode(node3);
DestoryNode(node4);
DestoryNode(node5);
} // 输入的两个链表均为空链表
[TestMethod]
public void FindTest6()
{
Node actual = CommonNodeHelper.FindFirstCommonNode(null, null);
Assert.AreEqual(actual, null);
}
}
3.2 测试结果:用例通过情况与代码覆盖率
(1)用例通过情况

(2)代码覆盖率

剑指Offer面试题:31.两个链表的第一个公共节点的更多相关文章
- 剑指offer 面试题52. 两个链表的第一个公共节点
这题之前leetcode做过,权当复习 首先这题没说是否一定有公共节点,如果代码可能因为这一点造成死循环的,需要提前验证所给两个链表是否有公共节点. 方法1:对于每一个list1的节点,遍历list2 ...
- 剑指Offer - 九度1505 - 两个链表的第一个公共结点
剑指Offer - 九度1505 - 两个链表的第一个公共结点2013-11-24 20:09 题目描述: 输入两个链表,找出它们的第一个公共结点. 输入: 输入可能包含多个测试样例.对于每个测试案例 ...
- 【剑指Offer】36、两个链表的第一个公共结点
题目描述: 输入两个链表,找出它们的第一个公共结点. 解题思路: 本题首先可以很直观的想到蛮力法,即对链表1(假设长度为m)的每一个结点,遍历链表2(假设长度为n),找有没有与其相同的 ...
- 【剑指Offer】面试题52. 两个链表的第一个公共节点
题目 输入两个链表,找出它们的第一个公共节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...
- 《剑指offer》面试题52. 两个链表的第一个公共节点
问题描述 输入两个链表,找出它们的第一个公共节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], lis ...
- LeetCode 面试题52. 两个链表的第一个公共节点
题目链接:https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/ 输入两个链表 ...
- 【剑指offer】面试题 52. 两个链表的第一个公共结点
面试题 52. 两个链表的第一个公共结点 NowCoder 题目描述 输入两个链表,找出它们的第一个公共结点. Java 实现 ListNode Class class ListNode { int ...
- 剑指 Offer 52. 两个链表的第一个公共节点 + 链表 + 第一个公共结点 + 双指针
剑指 Offer 52. 两个链表的第一个公共节点 Offer_52 题目详情 题解分析 可以使用两个指针 node1,node2 分别指向两个链表 headA,headB 的头结点,然后同时分别逐结 ...
- 【剑指offer】52. 两个链表的第一个公共节点
剑指 Offer 52. 两个链表的第一个公共节点 知识点:链表: 题目描述 输入两个链表,找出它们的第一个公共节点. 如下面的两个链表: 示例 示例1: 输入:intersectVal = 8, l ...
随机推荐
- Solr与MySQL查询性能对比
本文简单对比下Solr与MySQL的查询性能速度. 测试数据量:10407608 Num Docs: 10407608 这里对MySQL的查询时间都包含了从MySQL Server获取数据的时 ...
- 序言<EntityFramework6.0>
Entity Framework是微软战略性的数据访问技术,不同与早期访问技术,Entity Framework并不耦合在Visual Studio中,它提供了一个全面的, 基于模型的生态系统,使您能 ...
- XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate
namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...
- 如何快速找到排好序的数组中最先不连续的数字N
现在有一大堆自然数组成的小到大数组arr,其中会有123456910 这样就要找到6(最先不连续的数字) 举例:[12356789] 找到3 [012345678] 找到8 第一种:遍历数组判断是否 ...
- NetMQ(四): 推拉模式 Push-Pull
ZeroMQ系列 之NetMQ 一:zeromq简介 二:NetMQ 请求响应模式 Request-Reply 三:NetMQ 发布订阅模式 Publisher-Subscriber 四:NetMQ ...
- OpenCV特征点检测------ORB特征
OpenCV特征点检测------ORB特征 ORB是是ORiented Brief的简称.ORB的描述在下面文章中: Ethan Rublee and Vincent Rabaud and Kurt ...
- Quartz任务调度基本使用
转自:http://www.cnblogs.com/bingoidea/archive/2009/08/05/1539656.html 上一篇:定时器的实现.Java定时器Timer和Quartz介绍 ...
- 【ajax 提交表单】多种方式的注意事项
在业务中,可能因为表单内容过于庞大,字段过于繁杂,如果人为去拼接的话 ,需要耗费大量的时间和精力,与此同时,代码看上去也是冗余不堪. 所以,提交表单的时候如果能整个表单数据整体提交,那是非常开心的事情 ...
- Java工程师层级
- 截取QueryString 通过截取?和& 小写
$(function () { var url = location.href.replace("#", ""); var paraString = url.s ...