51-Intersection of Two Linked Lists
- Intersection of Two Linked Lists My Submissions QuestionEditorial Solution
Total Accepted: 72580 Total Submissions: 239762 Difficulty: Easy
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.
思路:两条链表如果相交,那从交点开始,后面一定都是相等的
那如何求第一个交点呢?
时间复杂度:O(n)
要么返回空
有交点时,说明尾部是对齐的,要找到第一个交点,只要让长的链表先走len1-len2步,这里假设len1是那条长链,那么此时再同时走,相遇的第一个节点便是交点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA==NULL||headB==NULL)return NULL;//有一为空返回
if(headA==headB)return headA;//头部相同返回
ListNode *l1=headA,*l2=headB;
int len1=1,len2=1;
while(l1->next){//遍历记录长度
l1=l1->next;
len1++;
}
while(l2->next){
l2=l2->next;
len2++;
}
ListNode *p = len1>len2?headA:headB; //p为长链表
ListNode *psmall=len1>len2?headB:headA; //psmall为短链表
int count=abs(len2-len1);
while(count--){
p=p->next;
}
while(p!=NULL&&psmall!=p){
p=p->next;
psmall = psmall->next;
}
if(p!=NULL)return p; //有交点
else return NULL; //无交点
}
};
51-Intersection of Two Linked Lists的更多相关文章
- [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 ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- LeetCode: Intersection of Two Linked Lists 解题报告
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(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- LeetCode_160. Intersection of Two Linked Lists
160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- [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 ...
- 【leetcode】Intersection of Two Linked Lists
题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
随机推荐
- Gitflow branch与Docker image tag命名冲突怎么办?
谷歌还是比必应要好用一点. 在前公司,我根据主流的git flow 给团队搭建了一套devops流程,运行在 docker & k8s上. 在现代devops流程中,一般推荐使用git分支名或 ...
- 状压dp学习笔记(紫例题集)
P3451旅游景点 Tourist Attractions 这个代码其实不算是正规题解的(因为我蒟蒻)是在我们的hzoj上内存限制324MIB情况下过掉的,而且经过研究感觉不太能用滚动数组,所以那这个 ...
- Vagrant 搭建开发环境实践
介绍 Development Environments Made Easy -官网标题 vagrant是一个命令行的虚拟机管理程序.用于简化搭建开发环境. vagrant使用ruby语言基于Chef ...
- 有关fgetc配合feof逐行读取文件最后一行读取两遍的错觉?
最近在做一个wifiap设置的接口,用户首先获取到当前wifi 热点的ssid 和pwd,然后修改,保存. 获取信息的时候是fopen对应的hostapd.conf文件,逐行读取,查找匹配的参数. 修 ...
- 表示数值的字符串 牛客网 剑指Offer
表示数值的字符串 牛客网 剑指Offer 题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2"," ...
- Python import urllib2 ImportError: No module named 'urllib2'
python3 import urllib2 import urllib2 ImportError: No module named 'urllib2' python3.3里面,用urllib.req ...
- let that = this用法解析
这种情况就是在一个代码片段里this有可能代表不同的对象,而编码者希望this代表最初的对象
- LeetCode刷题 树专题
树专题 关于树的几个基本概念 1 树的节点定义 2 关于二叉树的遍历方法 2.1 前序遍历 2.2 中序遍历 2.3 后序遍历 2.4 层序遍历 3 几种常见的树介绍 3.1 完全二叉树 3.2 二叉 ...
- 几个你不知道的dubbo注册中心细节
你会正确配置backup地址吗? 在配置dubbo注册中心时,一般会这样写 dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0 ...
- SpringCloud升级之路2020.0.x版-37. 实现异步的客户端封装配置管理的意义与设计
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 为何需要封装异步 HTTP 客户端 WebClient 对于同步的请求,我们使用 spri ...