[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 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.
问题:判断两个列表是否有相交的元素,若有,找出相交节点。
这题也是一道基础题,看了自己的列表知识还需要巩固下才好。
分别求出两个列表的长度 len1, len2 ,以及他们的长度差异 diff
跳过长度差异部分,对于剩余的相同长度部分,依次检查两个链表的对应节点,若又存在相交节点,则必有两个对应节点相等。
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { int len1 = ;
ListNode* p1 = headA;
while(p1 != NULL){
p1 = p1->next;
len1++;
} int len2 = ;
ListNode* p2 = headB;
while(p2 != NULL){
p2 = p2->next;
len2++;
} p1 = headA;
p2 = headB;
if (len1 > len2){
int diff = len1 - len2;
while(diff > ){
p1 = p1->next;
diff--;
}
} if (len2 > len1){
int diff = len2 - len1;
while(diff > ){
p2 = p2->next;
diff--;
}
} while(p1 != NULL ){
if ( p1 == p2){
return p1;
}
p1 = p1->next;
p2 = p2->next;
} return NULL;
}
参考资料:
LeetCode: Intersection of Two Linked Lists 解题报告, Yu's garden
[LeetCode] 160. Intersection of Two Linked Lists 解题思路的更多相关文章
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...
- 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 ...
- 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. ...
- 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 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 求两个链表的起始重复位置 --------- java
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 ...
随机推荐
- url参数中有+、空格、=、%、&、#等特殊符号的处理
url参数中有+.空格.=.%.&.#等特殊符号的问题解决? 解决办法: 将这些字符转化成服务器可以识别的字符,对应关系如下: URL字符转义 + URL 中+号表示空格 %2B 空格 URL ...
- 【代码优化】equals深入理解
覆盖equals时,遵守通用约定 对equal方法的覆盖看起来非常easy,可是有很多情况是容易导致错误,最好的避免这些错误的办法 就是不覆盖equals方法. 必须遵循的原则: 自反性--对于不论什 ...
- 详述iOS国际化
在真正将国际化实践前,只知道通过NSLocalizedString方法将相应语言的字符串加载进来即可.但最近公司项目的新需求增加英文版本,并支持应用内无死角切换~,这才跳过各种坑实现了应用内切换语言, ...
- Android 用ping的方法判断当前网络是否可用
判断网络的情况中,有个比较麻烦的情况就是连上了某个网络,但是那个网络无法上网 ,,, = = 想到了用ping指令来判断,经测试,可行~ ~ ~ private static final boolea ...
- hdu3368之DFS
Reversi Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- linux nohup命令
nohup 命令 用途:不挂断地运行命令.如果你正在执行一个job,并且你希望在退出帐户/关闭终端之后继续运行,可以使用nohup命令.nohup就是不挂起的意思( no hang up). 语法:n ...
- Jquery插件-Html5图片上传并裁剪
/** * 图片裁剪 * @author yanglizhe * 2015/11/16 */ (function($){ /** * Drag */ var Drag={obj:null,init:f ...
- Android开发手记(11) 滑动条SeekBar
安卓滑动条的操作特别简单,通过getProgress()可以获得SeekBar的位置,通过setProgress(int progress)可以设置SeekBar的位置.要想动态获取用户对SeekBa ...
- discuznt学习笔记
DBWR=DbHelper(client) Discuz.Data部分 DbHelper相当与抽象工厂中的Client,其中定义了需要与数据库进行操作的通用方法(如ExecuteScalar,Fi ...
- image控件读取数据库二进制图片
DataGridShowImage.aspx <%@ Page language="c#" debug="true" Codebehind=" ...