✡ 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 ...
随机推荐
- Linux-设置环境变量
一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量.例如我的mips-linux-gcc编译器在“ /opt/au1200_rm/build_tools/bin”目录下, ...
- redis2.8--内存管理
总而言之,redis内存管理是采用主要由操作系统自主控制内存分配,辅之以简单封装,达到简单且稍微改良的性能. 内存块,标记上本块size 如上图所示, 当调用zmalloc/zmalloc时,输入参数 ...
- 建议入门-用ArcMap进行空间查询与空间连接
1.打开arcmap并导入数据(如本图导入美国地图(usa.mxd)): 2.空间查询操作,在地图上的某片区域点击右键,得到下图,点击identify,此时我在阿拉斯加上面点击的 地图会闪现一下被查询 ...
- mybatis分页插件PageHelper的使用(转)
Mybatis 的分页插件PageHelper-4.1.1的使用 Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_P ...
- C# HttpBrowser 跨进程访问,解决内存泄露问题
#undef DEBUG using Microsoft.Win32; using Newtonsoft.Json; using System; using System.Collections.Ge ...
- python利用or在列表解析中调用多个函数.py
python利用or在列表解析中调用多个函数.py """ python利用or在列表解析中调用多个函数.py 2016年3月15日 05:08:42 codegay & ...
- scanf
scanf函数: (1)与printf函数一样,都被定义在头文件stdio.h里,因此在使用scanf函数时要加上#include <stdio.h>.它是格式输入函数,即按用户指定的格式 ...
- combox源码解析
/** * jQuery EasyUI 1.3.2 * * Copyright (c) 2009-2013 www.jeasyui.com. All rights reserved. * * Lice ...
- Matlab单一变量曲线拟合-cftool
2.启动曲线拟合工具箱>cftool 3.进入曲线拟合工具箱界面“Curve Fitting tool”(1)点击“Data”按钮,弹出“Data”窗口:(2)利用X data和Y data的下 ...
- php大力力 [022节]php编程要有一种态度:渴望遇见麻烦
2015-08-27 php大力力022.php编程要有一种态度:渴望遇见麻烦 不能一遇到问题和麻烦,就烦躁焦躁. 写程序,写代码,调试实验就是天天遇见不可预期的错误bug,这是常态.老生常谈,要适应 ...