两个链表的交叉 · 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.
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
节点相交不是节点相等,可见读题很重要。节点是否相等用等号即可。
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 统计链表长度的时候,length要加,但是node不能忘了动,都要写
- 二者齐头并进之后,不需要加提前退出的corner case,就算没有也是最后才退出。应该想好
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
读懂题目很重要
[复杂度]:Time complexity: O(n) Space complexity: O(1)
还是原来的链表,缩短 扫一遍 返回,没有新建别的链表。符合就地取材原则,所以是1
[英文数据结构或算法,为什么不用别的数据结构或算法]:
两个for循环:可以直接判断
[关键模板化代码]:
//getLength
public int getLength(ListNode node) {
int length = 0;
while(node != null) {
length++;
node = node.next;
}
链表while(node != null)取长度
[其他解法]:
炫耀技巧,没必要
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/*
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//corner case
if (headA == null || headB == null) {
return null;
}
//keep the same length
int A_len = getLength(headA);
int B_len = getLength(headB); while (A_len > B_len) {
headA = headA.next;
A_len--;
}
while (A_len < B_len) {
headB = headB.next;
B_len--;
}
//find the same node
while (headA != headB) {
headA = headA.next;
headB = headB.next;
} return headA;
} //getLength
public int getLength(ListNode node) {
int length = 0;
while(node != null) {
length++;
node = node.next;
}
return length;
}
}
两个链表的交叉 · Intersection of Two Linked Lists的更多相关文章
- [LeetCode]求两个链表的焦点--Intersection of Two Linked Lists
标题题目地址 1.解题意 求解两个链表的焦点,这个交点并不是焦点的值相等,而是需要交点之后的数据是完全相等的. 落实到java层面,就是交点处的对象是同一个对象即可. ps:我最开始没有读懂题目,然后 ...
- 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 两个链表的交叉
题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...
- LintCode-380.两个链表的交叉
两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 注意事项 如果两个链表没有交叉,返回null. 在返回结果后,两个链表仍须保持原有的结构. 可假定整个链表结构中没有循环. 样例 下列两 ...
- [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(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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
随机推荐
- es6 中的generator函数控制流程
Generator函数跟普通函数的写法有非常大的区别: 一是,function关键字与函数名之间有一个星号: 二是,函数体内部使用yield语句,定义不同的内部状态(yield在英语里的意思就是“产出 ...
- c++类成员函数重载常量与非常量版本时避免代码重复的一种方法
c++有时候需要为类的某个成员函数重载常量与非常量的版本,定义常量版本是为了保证该函数可作用于常量类对象上,并防止函数改动对象内容.但有时两个版本的函数仅仅是在返回的类型不同,而在返回前做了大量相同的 ...
- 第24课 #pragma使用分析
#pragma是C语言留给编译器厂商进行扩展用的. 这个关键字在不同的编译器之间也许是不能够移植的. #pragma简介 #pragma message #pragma message打印的消息并不代 ...
- ubuntu16.04安装Nvidia显卡驱动、CUDA8.0和cudNN V6
Nvidia显卡驱动安装 在ubuntu搜索框输入 软件更新,打开 "软件和更新" 对话框,在 附加驱动里选择系统检测到的Nvidia驱动,应用更改,重启系统: 安装完成之后查看G ...
- 【消息队列值Beanstalk】beeanstalk初识
Beanstalk是一个高性能.轻量级的.分布式的.内存型的消息队列系统.最初设计的目的是想通过后台异步执行耗时的任务来降低高容量Web应用系统的页面访问延迟.其实Beanstalkd是典型的类Mem ...
- POJ2374 Fence Obstacle Course
题意 Language:Default Fence Obstacle Course Time Limit: 3000MS Memory Limit: 65536K Total Submissions: ...
- 写时复制和fork,vfork,clone
写时复制 原理: 用了“引用计数”,会有一个变量用于保存引用的数量.当第一个类构造时,string的构造函数会根据传入的参数从堆上分配内存,当有其它类需要这块内存时,这个计数为自动累加,当有类析构时, ...
- HttpHelp 请求帮助类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- security自动登陆
package*.security; import java.util.ArrayList; import javax.servlet.http.Cookie; import javax.servle ...
- FreeFileSync 4.2 发布,文件夹比较和同步工具
FreeFileSync 是一款开源的文件夹比较和同步工具,可用于 Win 和 Lin 平台,最近发布了 4.2 版本. FreeFileSync 采用双面板设计,使用方法很简单: 在左面版和右面版上 ...