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. ...
随机推荐
- 自定义注解结合切面和spel表达式
在我们的实际开发中可能存在这么一种情况,当方法参数中的某些条件成立的时候,需要执行一些逻辑处理,比如输出日志.而这些代码可能都是差不多的,那么这个时候就可以结合自定义注解加上切面加上spel表达式进行 ...
- Java并发:重入锁 ReentrantLock(一)
ReentrantLock 是一种可重入的互斥锁,它不像 synchronized关键字一样支持隐式的重进入,但能够使一个线程(不同的方法)重复对资源的重复加锁而不受阻塞. ReentrantLock ...
- 外网访问vm虚拟机
目录 一.准备 二.外网访问主机电脑 三.外网访问VM虚拟机 一.准备 外网ip:39.189.8.5 访问https://www.ip138.com 可以查询外网ip 内网主机ip:192.168. ...
- Ubuntu14.04安装ia32-libs报错
安装编译环境的时候报错 sudo apt-get install ia32-libs Reading package lists... Done Building dependency tree Re ...
- Luogu P2024 [NOI2001]食物链 | 并查集
题目链接 思路:并查集,因为一开始我们并不知道每一只动物是哪一个种类的,所以我们干脆建立三倍于n的空间,1~n这三分之一用来存第i只动物是A的情况,n+1~2n这三分之一用来存第(i-n)只动物是B的 ...
- 跟着老猫来搞GO,集跬步而致千里
上次博客中,老猫已经和大家同步了如何搭建相关的GO语言的开发环境,相信在车上的小伙伴应该都已经搞定了环境了.那么本篇开始,我们就来熟悉GO语言的基础语法.本篇搞定之后,其实期待大家可以和老猫一样,能够 ...
- Tcpdump 常用命令、参数记录
一.介绍 一个关于Centos Tcpdump 的个人工作总结. 二.参数介绍: 1. -i: 指定要进行抓包的网卡 2.-s0 :表示每个报文的大小是接收到的指定大小,如果没有这个选项,则超过 ...
- 对于multitaper多窗口谱估计的理解及步骤 (对应matlab中pmtm函数)谱减法相关
对于多窗口谱估计的理解 目录 对于多窗口谱估计的理解 0. 缘起 1. PMTM 含义 2. 与我们常用的周期谱估计的区别 3. 计算过程 5. 多窗/单窗谱估计结果对比 6. 程序如何生成多窗 - ...
- jenkins持续集成Allure生成报表+邮件推送
本次基于<jenkins 生成HTML报表,邮件推送>的基础上将生成HTML报表修改为Allure生成报表,可以参考官方文档:https://docs.qameta.io/allure/# ...
- OpenXml SDK学习笔记(4):设置文件级别的样式
观察上一段日记最后的代码: 这里的样式基本可以理解为行内CSS.那么既然有行内的样式,就肯定有外部的样式.那这部分就对应笔记1里说的style.xml文件.这个文件对应的是Document.MainD ...