LeetCode: Intersection of Two Linked Lists 解题报告
Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
begin to intersect at node c1.
Notes:
If the two linked lists have no intersection at all, return null.
The linked lists must retain their original structure after the function returns.
You may assume there are no cycles anywhere in the entire linked structure.
Your code should preferably run in O(n) time and use only O(1) memory.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
SOLUTION 1:
1. 得到2个链条的长度。
2. 将长的链条向前移动差值(len1 - len2)
3. 两个指针一起前进,遇到相同的即是交点,如果没找到,返回null.
相当直观的解法。空间复杂度O(1), 时间复杂度O(m+n)
public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
} int lenA = getLen(headA);
int lenB = getLen(headB); if (lenA > lenB) {
while (lenA > lenB) {
headA = headA.next;
lenA--;
}
} else {
while (lenA < lenB) {
headB = headB.next;
lenB--;
}
} while (headA != null) {
if (headA == headB) {
return headA;
}
headA = headA.next;
headB = headB.next;
} return null;
} public int getLen(ListNode node) {
int len = 0;
while (node != null) {
len++;
node = node.next;
}
return len;
}
2014.12.17 redo:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
} ListNode cur = headA;
int len1 = getLen(headA);
int len2 = getLen(headB); int cnt = Math.abs(len1 - len2); // cut the longer list.
if (len1 > len2) {
while (cnt > 0) {
headA = headA.next;
cnt--;
}
} else {
while (cnt > 0) {
headB = headB.next;
cnt--;
}
} while (headA != null) {
if (headA == headB) {
return headA;
} headA = headA.next;
headB = headB.next;
} return null;
} public int getLen(ListNode head) {
int cnt = 0;
while (head != null) {
head = head.next;
cnt++;
} return cnt;
}
}
SOLUTION 2:
解完后,打开Leetcode的solution, 找到一个很巧妙的解法。其实与解法1相比应该快不了多少,但是写出来超有B格的。。
Two pointer solution (O(n+m) running time, O(1) memory):
Maintain two pointers pA and pB initialized at the head of A and B, respectively. Then let them both traverse through the lists, one node at a time.
When pA reaches the end of a list, then redirect it to the head of B (yes, B, that's right.); similarly when pB reaches the end of a list, redirect it the head of A.
If at any point pA meets pB, then pA/pB is the intersection node.
To see why the above trick would work, consider the following two lists: A = {1,3,5,7,9,11} and B = {2,4,9,11}, which are intersected at node '9'. Since B.length (=4) < A.length (=6), pB would reach the end of the merged list first, because pB traverses exactly 2 nodes less than pA does. By redirecting pB to head A, and pA to head B, we now ask pB to travel exactly 2 more nodes than pA would. So in the second iteration, they are guaranteed to reach the intersection node at the same time.
If two lists have intersection, then their last nodes must be the same one. So when pA/pB reaches the end of a list, record the last element of A/B respectively. If the two last elements are not the same one, then the two lists have no intersections.
主页君实现如下:
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
} ListNode pA = headA;
ListNode pB = headB; ListNode tailA = null;
ListNode tailB = null; while (true) {
if (pA == null) {
pA = headB;
} if (pB == null) {
pB = headA;
} if (pA.next == null) {
tailA = pA;
} if (pB.next == null) {
tailB = pB;
} //The two links have different tails. So just return null;
if (tailA != null && tailB != null && tailA != tailB) {
return null;
} if (pA == pB) {
return pA;
} pA = pA.next;
pB = pB.next;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/GetIntersectionNode1.java
附一个链表大总结的链接:
http://weibo.com/3948019741/BseJ6ukI3
LeetCode: Intersection of Two Linked Lists 解题报告的更多相关文章
- 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...
- 【原创】leetCodeOj --- Intersection of Two Linked Lists 解题报告(经典的相交链表找交点)
题目地址: https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 题目内容: Write a program to fi ...
- [LeetCode] 160. Intersection of Two Linked Lists 解题思路
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...
- LeetCode——Intersection of Two Linked Lists
Description: Write a program to find the node at which the intersection of two singly linked lists b ...
- [LeetCode] Intersection of Two Linked Lists 两链表是否相交
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Hibernate日期映射类型
映 射 类 型 Java类型 标准SQL类型 描 述 date java.util.Date或者java.sql.Date DATE 代表日期,形式为: YYYY-MM-DD time java ...
- CMake 基本用法--写CMakeList.txt
http://techbase.kde.org/Development/Tutorials/CMake_(zh_CN) http://www.cmake.org/Wiki/CMake 这一章将从软件开 ...
- HighCharts/Highstock使用小结,使用汉化及中文帮助文档
此文档是本人在开发过程图形报表时使用HighCharts所遇到的问题及解决方案 .最后附上有HighCharts中文帮助文档 HighCharts 版本:Highcharts-3.0.1 Hi ...
- spring注解 di 和 ioc 注解
注解: 1.注解就是为了说明java中的某一个部分的作用(Type) 2.注解都可以用于哪个部门是@Target注解起的作用 3.注解可以标注在ElementType枚举类所指定的位置上 4. @Do ...
- java socket输入输出中文乱码问题
http://hi.baidu.com/linjk03/item/e2028bfd990c14ea1a111feb 统一了输入输出的编码格式,是不会有乱码问题出现的. 构造Reader或Write ...
- js LINQ教程
在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...
- go 学习 ---数据类型
25个关键字 程序声明:import, package 程序实体声明和定义:chan, const, func, interface, map, struct, type, var 程序流程控制:go ...
- Win32:引用头文件
1.首先,在代码文件头部使用#include来包含任何头文件(扩展名为.h或者.hpp的文件)都不会“产生”额外的函数. 我们可以说,包含一个头文件之后,该头文件内部已经定义的一些变量,宏,函数等等资 ...
- OAF_OAF OAWebBean和OAPageContext的分析(概念)
2015-04-03 Created By BaoXinjian
- 局域网不同用户同时登录同一个网站,会出现session乱窜的问题
出现这种问题的情景再现: 1.有一部分人访问网站会出现session乱窜的问题. 2.这部分人是在同一个局域网中. 3.不同菜单看到的信息是不同人的,或者同一个菜单翻页时有的时候看到的是自己的数据,有 ...