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. 题意:就是求取两个链表的交汇点,
要求:无交汇点返回NULL,否则返回交汇点的地址。
暴力思路:O(nm),固定A链表的第一个点,依次遍历B链表看是否有相同的点;接着固定A链表的第二个点,再依次遍历B链表,直到找到相同点为止。
hash解:时间复杂度O(n+m),空间O(n)或者O(m)
两指针解法:我们发现只要两个链表长度一样,就只需同时后移节点指针比较一个,若其中一个较长呢,其实处理一下,把两个链表变成一样长即可。
解法步骤:
1.求出两个链表的长度
2.若一样长,无需处理;若其中一个较长,则只需让较长的链表先走abs(lengthA-lengthB)步即可。
3.同时后移节点指针,直到找到交汇点。
代码:
class Solution {
private:
int getLength(ListNode* head){
if(head==NULL) return ;
ListNode* p=head;
int res=;
while (p->next!=NULL)
{
++res;p=p->next;
}
return res;
}
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA==NULL||headB==NULL) return NULL;
int length_A=getLength(headA);
int length_B=getLength(headB);
ListNode* pA=headA;
ListNode* pB=headB;
int dis=;
if (length_A>length_B)
{
dis=length_A-length_B;
while (dis>)
{
--dis;
pA=pA->next;
}
}else if(length_A<length_B)
{
dis=length_B-length_A;
while (dis>)
{
--dis;
pB=pB->next;
}
}
while (pA!=NULL&&pB!=NULL&&pA!=pB)
{
pA=pA->next;
pB=pB->next;
}
if(pA==pB&&pA!=NULL) return pA;
else return NULL;
}
};
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 ...
- [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--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- 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] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- Excel数据导入SQL Server
基本有2种方案,都是无需安装Office的方案 Ole DB读取 + BulkCopy 获取Excel各个SheetName //连接串 string strConn = "Provider ...
- 实用工具特别推荐 BGInfo
https://docs.microsoft.com/en-us/sysinternals/downloads/bginfo 介绍 您在办公室中走过多少次,需要点击几个诊断窗口,提醒自己其配置的重要方 ...
- MongoDB最简单的入门教程之一 环境搭建
MongoDB是近年来非常流行的一个介于关系数据库和非关系数据库之间的解决方案,特别广泛地应用于国内很多互联网公司,是非关系数据库当中功能最丰富,最像关系数据库的. MongoDB支持的数据结构非常松 ...
- example - 在这里插入一句话的简介
总览 (SYNOPSIS) example [options] arguments 描述 (DESCRIPTION) 在这里插入描述 man9 应当是 “内核文档” 但是由于内核文档一般不以 man ...
- k8s集群之Docker安装镜像加速器配置与k8s容器网络
安装Docker 参考:https://www.cnblogs.com/rdchenxi/p/10381631.html 加速器配置 参考:https://www.cnblogs.com/rdchen ...
- spring boot jar的生成
1)准备demo 2)打开idea项目结构 3)添加 4)按顺序 6)bulid 7)完成 查看out文件
- c/s端测试——nw.js篇(selenium工具)
最近在为兄弟部门开发自动化测试工具. 然后才知道现在竟然有JS工具可以把JS打包成cs端程序了,太牛了,js发展是真快.并且还是跨平台的,mac.windows.linux都支持. 当然,今天不是说n ...
- 深入Linux内核架构——进程管理和调度(上)
如果系统只有一个处理器,那么给定时刻只有一个程序可以运行.在多处理器系统中,真正并行运行的进程数目取决于物理CPU的数目.内核和处理器建立了多任务的错觉,是通过以很短的间隔在系统运行的应用程序之间不停 ...
- 如何用纯 CSS 创作一个容器厚条纹边框特效
效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/YLqbXy 可交互视频教程 此视 ...
- mac 监控文件变化并重启php
自己撸一个框架,需要监控代码变化 安装fswatch brew install fswatch shell重启PHP脚本reload.sh #!/bin/sh do ps -ef | grep php ...