[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 ...
随机推荐
- 机房管理系统——vb与excel链接2
因为我之前找的文档让我以为在创建表里面走了非常大的误区,所以当时我直接就在学生管理系统的目录里建了张表,执行时候直接打开这样表即可了. 可是这里面还是存在着非常大的误区. 后来我看了周坤的博客感觉他比 ...
- rman catalog (rman 恢复目录)
受控制文件大小的限制,一般rman需要用rman catalog来管理及存放备份信息: 这里介绍一下创建rman catalog的步骤: C:\Documents andSettings\Admini ...
- AsMVC:一个简单的MVC框架的Java实现
当初看了<从零开始写一个Java Web框架>,也跟着写了一遍,但当时学艺不精,真正进脑子里的并不是很多,作者将依赖注入框架和MVC框架写在一起也给我造成了不小的困扰.最近刚好看了一遍sp ...
- [转] stat命令输出结果中, Access,Modify,Change的含义
先建立一个空白文件a.txt 1 [emduser@emd tmp]$ touch a.txt 2 3 [emduser@emd tmp]$ ls -al a.txt 4 5 -rw-rw-r ...
- Android面试,与Service交互方式
五种交互方式,分别是:通过广播交互.通过共享文件交互.通过Messenger(信使)交互.通过自定义接口交互.通过AIDL交互.(可能更多) Service与Thread的区别 Thread:Thre ...
- Java基础知识强化98:Apache和Tomcat服务器的联系与区别
1. Apache 和 Tomcat 都是web网络服务器,两者既有联系又有区别,在进行HTML.PHP.JSP.Perl等开发过程中,需要准确掌握其各自特点,选择最佳的服务器配置. Apache是w ...
- Java基础知识强化72:正则表达式之判断功能(手机号码判断 和 校验邮箱)
1. 判断功能: 使用了String类的matches方法,如下: public boolean matches(String regex): 2. 判断手机号码的案例: package cn.it ...
- JAVA try-catch-finally-return
正常执行流程: try执行,遇到异常就跳到catch执行(以使得程序不会崩溃): 不管有没有异常catch,最后都执行finally 含return语句执行流程分析: 若try块中return ...
- nyoj 756 重建二叉树
重建二叉树主要是给你一颗二叉树的前序遍历的结果和中序遍历的结果或者后序遍历的结果或者中序遍历的结果,让你求出其中的后序遍历的结果或者前序遍历的结果,这里知道其中的两个就能求出第三个,但是知道的两个必须 ...
- pbxproj文件冲突解决办法
企业开发经常会遇到project.pbxproj文件冲突的问题 project.pbxproj文件主要包含了以下几项主要信息 工程文件关联信息,如PBXBuildFile.PBXFileReferen ...