LeetCode(39)-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.
思路:
- 首先这道题目的意思是判断两条链表是不是有重叠的部分,如果有返回那个交叉的节点,如果没有返回null
- 这道题目的思路是:如果有交叉,最后肯定有相同的部分,如上面那样
- 举个例子,a和b两头链条,a.length = 7,b.length = 5,用两个指针listA和listB,分别指向a和b的head,listA往后两个,开始listA和listB同步往后移动开始比较,有相同的就有交叉,返回那个节点
-
代码:
/**
* 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) {
ListNode a = headA;;
ListNode b = headB;;
int lengthA = 0;
int lengthB = 0;
int n = 0;
while(a != null){
a = a.next;
lengthA++;
}
while(b != null){
b = b.next;
lengthB++;
}
if(lengthA > lengthB){
n = lengthA - lengthB;
a = headA;
b = headB;
while( n > 0){
n--;
a = a.next;
}
}else{
n = lengthB -lengthA;
a = headA;
b = headB;
while(n > 0){
n--;
b = b.next;
}
}
while(a != b){
a = a.next;
b = b.next;
}
return a;
}
}
LeetCode(39)-Intersection of Two Linked Lists的更多相关文章
- [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 (两个链表的交点)
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. ...
- 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 ...
- [leetCode][003] Intersection of Two Linked Lists
[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- ✡ 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 ex ...
随机推荐
- Strom数据流分组解析
本文可作为 <<Storm-分布式实时计算模式>>一书1.5节的读书笔记 数据流分组定义了一个数据流中的tuple如何分发给topology中不同bolt的task. Shuf ...
- 内存管理单元--MMU
现代操作系统普遍采用虚拟内存管理(Virtual Memory Management)机制,这需要处理器中的MMU(Memory Management Unit,内存管理单元)提供支持,本节简要介绍M ...
- EBS密码加密研究
DECLARE v_password_1 VARCHAR2(240); v_password_2 VARCHAR2(240); v_password_3 VARCHAR2(240); ...
- Android最佳实践之SystemBar状态栏全版本适配方案
前言 自从MD设计规范出来后,关于系统状态栏的适配越受到关注,因为MD在5.0以后把系统状态栏的颜色改为可由开发者配置的,而在5.0之前则无法指定状态栏的颜色,所以这篇就说说使用Toolbar对系统状 ...
- Hash冲突解决
hash的冲突不可避免的 1.开放地址法 开放地执法有一个公式:Hi=(H(key)+di) MOD m i=1,2,-,k(k<=m-1) 其中,m为哈希表的表长.di 是产生冲突的时候的增量 ...
- int(*p)[]和int(**p)[]
1. int(*p)[10]: 根据运算符的结合律,()的优先级最高,所以p是一个指针,指向的一个维度为10的一维数组. p一个指向数组的某一行 int a[1][4]={1,2,3,4}; int ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十一)
机器人发射子弹已经完成了,下面看看怎么给玩家设置障碍. 大家从上篇的图可以看到,在机器和篮筐直接有若干障碍物.我们先看如何实现它们. 打开SpriteBuilder,在Sprites文件夹中新建Sma ...
- SMEM介绍
SMEM :shared memory,是高通平台各子系统共享信息的一种机制,通过SMEM机制,PBL可以将信息传递给SBL1,SBL1可以将信息传递给RPM.LK.下面分析一个SMEM信息传递的具体 ...
- jenkin插件整理
分类 plugin名称 wiki地址 源码地址 plugin作用范围 备注 Build Reports构建报告(此类插件用来分析构建结果,比果代码检查,测试CASE分析,并将这些结果以报表,趋势图等形 ...
- VC2010工程依赖不再自动链接
发现 VC2010 Express 设置了 Project Dependencies 之后并没有自动链接. 而在VC2008中工程依赖不仅影响构建顺序,也会自动链接依赖项. 具体说明见: http: ...