1. 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的更多相关文章

  1. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  2. [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 ...

  3. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

    160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...

  9. [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 ...

  10. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

随机推荐

  1. [对对子队]会议记录4.15(Scrum Meeting 6)

    今天已完成的工作 何瑞 ​ 工作内容:制作了合成指南 ​ 相关issue:实现游戏内UI界面使用的组件 马嘉 ​ 工作内容:基本实现了箱子内物品列表 ​ 相关issue:实现游戏内UI界面使用的组件 ...

  2. 【二食堂】Alpha - Scrum Meeting 3

    Scrum Meeting 3 例会时间:4.13 12:00 - 12:30 进度情况 组员 昨日进度 今日任务 李健 1. 继续学习前端知识,寻找一些可用的框架.issue 1. 搭建主页html ...

  3. TCP/IP参考模型(应用层、传输层、网际层、网络接口层)、五层参考模型(应用层、传输层、网络层、数据链路层、物理层)、OSI与TCP/IP参考模型比较

    文章转自:https://blog.csdn.net/weixin_43914604/article/details/104597450 学习课程:<2019王道考研计算机网络> 学习目的 ...

  4. 常用JAVA API :String 、StringBuilder、StringBuffer的常用方法和区别

    摘要 本文将介绍String.StringBuilder类的常用方法. 在java中String类不可变的,创建一个String对象后不能更改它的值.所以如果需要对原字符串进行一些改动操作,就需要用S ...

  5. Spring Security:简单的保护一个SpringBoot应用程序(总结)

    Spring Security 在 Java类中的配置 在 Spring Security 中使用 Java配置,可以轻松配置 Spring Security 而无需使用 XML . 在Spring ...

  6. Java:检查异常与未检查异常

    一.异常的介绍 Throwable 是 Java 中所有错误和异常的超类.Java 虚拟机仅抛出属于此类(或其子类之一)的实例对象,或者是 throw 语句也可以抛出该对象.同样,catch 子句中的 ...

  7. 有关fgetc配合feof逐行读取文件最后一行读取两遍的错觉?

    最近在做一个wifiap设置的接口,用户首先获取到当前wifi 热点的ssid 和pwd,然后修改,保存. 获取信息的时候是fopen对应的hostapd.conf文件,逐行读取,查找匹配的参数. 修 ...

  8. 分布式事务(四)之TCC

    在电商领域等互联网场景下,传统的事务在数据库性能和处理能力上都暴露出了瓶颈.在分布式领域基于CAP理论以及BASE理论,有人就提出了柔性事务的概念.在业内,关于柔性事务,最主要的有以下四种类型:两阶段 ...

  9. Pip安装Django超时(time out)解决方法

    (ll_env)learning_log$  pip install Django  执行该命令,始终报错,如上图 解决方法如下: pip install  -i http://pypi.douban ...

  10. 南大《软件分析》课程笔记——Data Flow Analysis

    南大<软件分析>--Data Flow Analysis @(静态分析) 目录 数据流分析概述 数据流分析应用 Reaching Definitions Analysis(may anal ...