✡ 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 ...
随机推荐
- jsoup Cookbook(中文版)--爬虫(java)
转载:http://www.open-open.com/jsoup/ 目录: 入门 解析和遍历一个html文档 输入 解析一个html字符串 解析一个body片断 根据一个url加载Document对 ...
- wp8.1 C#技巧: Data和ViewModel类编写
在Data.cs namespace PicApp { [DataContract] class DataItem : PropertyChangeNotification { public even ...
- data属性
本框架内置组件以及部分插件可以通过data属性来初始化并使用,通常通过data-toggle来调用API(toggle是触发器的意思,例如我们创建一个navtab标签可以通过为a的data-toggl ...
- linux安装时出现your cpu does not support long mode的解决方法
如果你确定你的电脑支持64bit且是64bit的宿主系统,则需要修改BIOS中的Inter Virtualization Technology为enabled.
- 《day15---多线程安全问题_JDK1.5的锁机制》
//15同步问题的分析案例以及解决思路 //两个客户到一个银行去存钱,每个客户一次存100,存3次. //问题,该程序是否有安全问题,如果有,写出分析过程,并定于解决方案. /* 发现运行结果: su ...
- FSMC stm32
1.FSMC机制 FSMC(Flexihie Static Memory Controller,可变静态存储控制器)是STM32系列中内部集成256 KB以上FlaSh,后缀为xC.xD和xE的高存储 ...
- SVG 2D入门1 - SVG综述
位图与矢量图 以前,浏览器中显示的图形,例如jpeg.gif等,都是位图,这些图像格式是基于光栅的.在光栅图像中,图像文件定义了图像中每个像素的颜色值.浏览器需要读取这些值并做出相应行动.这种图像的再 ...
- hdu 2096
PS:做不出前面几道题...很不爽..扒拉了几下找了简单题来做.... #include "stdio.h" int cal(int a); int main(){ int a,b ...
- application:didFinishLaunchingWithOptions:详解
iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...
- iOS探索:对NSArray中自定义的对象进行排序
http://mobile.51cto.com/hot-434804.htm 我们开发的每个程序都会使用到一些数据,而这些数据一般被封装在一个自定义的类中.例如一个音乐程序可能会有一个Song类,聊天 ...