✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java
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.
如果两个链表有重复的部分,那么返回重复的起始位置,否则,返回null。
两种方法:
1、(参考discuss中,并非最佳答案)
可以利用hashMap,先把第一个链表的所有节点放入map中,然后再遍历第二个链表,看map中是否有相同的节点。如果没有,返回null。
时间复杂度O(m+n),空间复杂度O(m) or O(n)
2、先遍历一次链表,找出连个链表的最后一个节点(如果不一样,那么返回null)以及长度差(最佳)
然后较长的链表先走长度差个节点,
之后两个链表一起走,遇到相同的就返回。
时间复杂度O(m+n),空间复杂度O(1)
/**
* 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) {
int len1 = 1,len2 = 1;
if( headA == null || headB == null)
return null;
ListNode node1 = headA;
ListNode node2 = headB; while( node1.next != null ){
node1 = node1.next;
len1++;
}
while( node2.next != null ){
node2 = node2.next;
len2++;
}
if( len1 == 0 || len2 == 0 || node1 != node2 )
return null;
node1 = headA;
node2 = headB;
if( len1 > len2 ){
while( len1 > len2 ){
len1--;
node1 = node1.next;
}
}else if( len1 < len2 ){
while( len1<len2){
len2--;
node2 = node2.next;
}
} while( len1 >0 ){
if( node1 == node2 )
return node1;
node1 = node1.next;
node2 = node2.next;
len1--;
}
return null;
}
}
3、(参考discuss)
不用第一次遍历找出长度差,一直循环遍历找出相同的节点(如果不存在会出现null)
/**
* 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) {
//boundary check
if(headA == null || headB == null) return null; ListNode a = headA;
ListNode b = headB; //if a & b have different len, then we will stop the loop after second iteration
while( a != b){
//for the end of first iteration, we just reset the pointer to the head of another linkedlist
a = a == null? headB : a.next;
b = b == null? headA : b.next;
} return a;
}
}
✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java的更多相关文章
- [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 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 ... 
- [LintCode] Intersection of Two Linked Lists  求两个链表的交点
		Write a program to find the node at which the intersection of two singly linked lists begins. Notice ... 
- [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]160.Intersection of Two Linked Lists(2个链表的公共节点)
		Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ... 
- [LeetCode]160. Intersection of Two Linked Lists判断交叉链表的交点
		方法要记住,和判断是不是交叉链表不一样 方法是将两条链表的路径合并,两个指针分别从a和b走不同路线会在交点处相遇 public ListNode getIntersectionNode(ListNod ... 
- 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 ... 
- Java for 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 ... 
随机推荐
- 端午小长假--前端基础学起来04CSS选择器
			定义: 选择器{ 样式: } 选择器指明{}中的样式的作用对象,即作用于网页中的哪些元素 <head><meta http-equiv="Content-Type" ... 
- 自定义的dialog
			自定义的dialog 其中包含置顶 删除 和取消 下面的是BaseDialog package com.free.csdn.view.dialog; import android.app.Dialo ... 
- iOS开发之通知使用总结
			通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发 ... 
- JAVA SERVLET专题(下)
			HTTP简介 ·WEB浏览器与WEB服务器之间的一问一答的交互过程必须遵守一定的规则,这个规则就是HTTP协议. ·HTTP是hypertext transfer protocol(超文本传输协议)的 ... 
- 微软TechEd2013大会门票热卖!
			微软TechEd2013大会将在北京.上海两地隆重举行! 会议时间安排如下: 北京:12月5日—6日 国家会议中心 上海:12月11日—12日 国际会议中心 现在是门票热卖时期,票价:2688.0 ... 
- 理解smart pointer之三:unique_ptr
			unique_ptr最先在boost中被定义,后来被C++标准委员会选中为C++11的feature之一. std::unique_ptr is a smart pointer that retain ... 
- 转:Java面试题集(1-50)
			Java程序员面试题集(1-50) http://blog.csdn.net/jackfrued/article/details/17403101 一.Java基础部分 1.面向对象的特征有哪些方面? ... 
- 转:115个Java面试题和答案——终极列表(上)
			转自:http://www.importnew.com/10980.html 本文我们将要讨论Java面试中的各种不同类型的面试题,它们可以让雇主测试应聘者的Java和通用的面向对象编程的能力.下面的 ... 
- lightoj1080 线段树
			//Accepted 6628 KB 520 ms //I a b 把a到b区间的二进制位去反,转化成a到b区间的数全部加1 //Q a 判断第a位的奇偶 #include <cstdio> ... 
- maven3在eclipse3.4.2中创建java web项目
			学习maven时参考的一些的博客地址:http://www.cnblogs.com/fnng/archive/2011/12/16/2290587.htmlhttp://sarin.iteye.com ... 
