Leetcode Intersection of Two Linked Lists
2015-09-11 12:36
411 查看
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
begin to intersect at node c1.
Notes:
If the two linked lists have no intersection at all, return
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.
1. 得到2个链条的长度。
2. 将长的链条向前移动差值(len1 - len2)
3. 两个指针一起前进,遇到相同的即是交点,如果没找到,返回null.
相当直观的解法。空间复杂度O(1), 时间复杂度O(m+n)
First calculate the length of two lists and find the difference. Then start from the longer list at the diff offset, iterate though 2 lists and find the node.
Java code:
Reference:
1. http://www.programcreek.com/2014/02/leetcode-intersection-of-two-linked-lists-java/
2. http://yucoding.blogspot.com/2014/12/leetcode-question-intersection-of-two.html
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.
解题思路:
一定要正确理解题意。Intersection 就是两个链表最后有完全相同的地方。所以要从相同长度开始计算。开始我没有正确理解题意,怎么也想不出来。后来看了答案才明白,简单明了。1. 得到2个链条的长度。
2. 将长的链条向前移动差值(len1 - len2)
3. 两个指针一起前进,遇到相同的即是交点,如果没找到,返回null.
相当直观的解法。空间复杂度O(1), 时间复杂度O(m+n)
First calculate the length of two lists and find the difference. Then start from the longer list at the diff offset, iterate though 2 lists and find the node.
Java code:
public ListNode getIntersectionNode(ListNode headA, ListNode headB) { //First calculate the length of two lists and find the difference. //Then start from the longer list at the diff offset, iterate though 2 lists and find the node. int len1 = 0; int len2 = 0; ListNode p1 = headA; ListNode p2 = headB; if(p1 == null || p2 == null) { return null; } while(p1!= null) { len1++; p1 = p1.next; } while(p2!= null) { len2++; p2 = p2.next; } int diff = 0; p1 = headA; p2 = headB; if(len1 > len2) { diff = len1 -len2; int i = 0; while(i< diff) { p1 = p1.next; i++; } }else { diff = len2- len1; int i = 0; while(i< diff) { p2 = p2.next; i++; } } while(p1 != null && p2 != null) { if(p1.val == p2.val) { return p1; } p1 = p1.next; p2 = p2.next; } return null; }
Reference:
1. http://www.programcreek.com/2014/02/leetcode-intersection-of-two-linked-lists-java/
2. http://yucoding.blogspot.com/2014/12/leetcode-question-intersection-of-two.html
相关文章推荐
- linux下简易socket编程
- 白话经典算法系列之六 快速排序 快速搞定
- 图像滤镜艺术---(Punch Filter)交叉冲印滤镜
- 图像滤镜艺术---(Nostalgla Filter)老照片滤镜
- 项目人力资源管理的思考
- LeetCode || Factorial Trailing Zeroes
- Java中转UTC时间字符串(含有T Z)为local时间
- UiTableview的常用小知识汇集
- 图像滤镜艺术---怀旧风格滤镜
- 遗传算法Demo
- 图像滤镜艺术---暗调滤镜
- 灵活强大的构建系统Gradle
- hdu1151最小路径覆盖
- Linux下软件常见安装方式
- swap文件
- yum报错
- ACM中素数(prime)操作入门解析
- TCP协议疑难杂症全景解析
- 【leetcode每日一题】226.Invert Binary Tree
- LeetCode ||Number of Digit One