index
title: 两个链表的第一个公共结点 date: 2019-08-21T11:00:41+08:00 draft: false categories: offer
题目
输入两个链表,找出它们的第一个公共结点。
解决思路
空间复杂度 O(n) 的算法
使用辅助容器,保存第一个链表的所有元素
遍历第二个链表,并对比当前节点是否在辅助容器中
/**
* 空间 O(n)
*
* @param pHead1
* @param pHead2
* @return
*/
public ListNode FindFirstCommonNode_1(ListNode pHead1, ListNode pHead2) {
Set<ListNode> node1s = new HashSet<>();
while (pHead1 != null) {
node1s.add(pHead1);
pHead1 = pHead1.next;
}
while (pHead2 != null) {
if (node1s.contains(pHead2)) {
return pHead2;
}
pHead2 = pHead2.next;
}
return null;
}空间复杂度 O(1) 的算法
由于两个链表有可能不一样长,首先通过遍历找到他们的长度
移动较长的那个链表,使得两个链表长度一致
同步遍历两个链表
原理:如果两个链表相交,那么它们一定有相同的尾节点
Last updated
Was this helpful?