题目:

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.

提示:

假设两个链表有合并的情况,那么合并部分的长度一定是一样的,在合并之前长度会有所不同,所以先求出长度,然后把长的链表向前走,让他们“在同一起跑线”,然后依次比较。

代码:

/**
* 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 || !headB) return NULL;
int lenA = , lenB = ;
ListNode *nodeA, *nodeB;
for (nodeA = headA; nodeA; nodeA = nodeA->next, ++lenA);
for (nodeB = headB; nodeB; nodeB = nodeB->next, ++lenB);
if (lenB > lenA) {
for (int i = ; i < lenB - lenA; ++i, headB = headB->next);
} else {
for (int i = ; i < lenA - lenB; ++i, headA = headA->next);
}
if (headA == headB) return headA;
while (headA && headB) {
headA = headA->next;
headB = headB->next;
if (headA == headB) return headA;
}
return NULL;
}
};

【LeetCode】160. Intersection of Two Linked Lists的更多相关文章

  1. 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...

  2. 【一天一道LeetCode】#160. Intersection of Two Linked Lists

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  3. 【原创】leetCodeOj --- Intersection of Two Linked Lists 解题报告(经典的相交链表找交点)

    题目地址: https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 题目内容: Write a program to fi ...

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

  5. 【leetcode❤python】 160. Intersection of Two Linked Lists

    #-*- coding: UTF-8 -*- #两种方法#方法1:#计算出A和B两个链表的长度分别为m.n;#长度长的链表先走m-n步,之后再一次遍历寻找#方法2:#先走到一个链表的尾部,从尾部开始走 ...

  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 解题思路

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

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

  9. 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】

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

随机推荐

  1. [笔记]我的Linux入门之路 - 03.Java环境搭建

    其实ubuntu是自带一个叫openJDK的东西的,是谷歌看Oracle不爽而搞的.不过呢...总感觉不太习惯,况且我既然都来Linux了,总是想折腾一把的. 首先先检查下有没有安装java.终端输入 ...

  2. python 使用 'python -m pip install --upgrade pip'提示PermissionError: [WinError 5] 拒绝访问

    执行pip install --upgrade pip 提示"PermissionError: [WinError 5] 拒绝访问",如下图,由于更新的用户权限不够,换成管理员运行 ...

  3. 【JAVAWEB学习笔记】14_response

    HttpServletResponse 学习目标 案例一.完成文件下载 案例二.生成验证码(了解) 1.HttpServletResponse概述 我们在创建Servlet时会覆盖service()方 ...

  4. winows 服务器环境搭建 (碰到了windows服务器,小记一下吧~)

    1.连接远程服务器  安装wamp 2.查看wamp 默认端口号是否与对应windows 服务器冲突,如果冲突,则改之 WAMP装好之后默认的端口是80,但是这个80端口呢,可以热门端口啊,迅雷,II ...

  5. shell群发邮件脚本

    linux版本:CentOS  6.7        //可以使用lsb_release -a查看 一.修改/etc/mail.rc set from=123456@qq.com //你自己的真实邮箱 ...

  6. 详谈JAVA中的file类与IO流

    File类 位置于java.io包构造方法:File(String parent, String child)new file("d:\\","a.txt") ...

  7. java面向对象--抽象类和接口

    如果某个父类只知道其子类应该包含哪些方法,但无法知道如何实现这些方法,同时要使用多态的特性,怎么办? 抽象方法:关键字abstract允许在一个类中创建一个或多个没有方法体的方法--只提供方法签名,但 ...

  8. bootstrap4中文版(纯手工翻译)

    1初步开始 1.1依赖 这个仓储包含一系列基于bootstrap标识和css样式的原生angular2指令.所以是不需要依赖jq和bootstrap.js的.只需要以下依赖即可: Angular(需要 ...

  9. JMETER 不同线程组 变量值 的参数传递

    线程组 1   在线程组1中使用__setProperty函数设置jmeter属性值(此值为全局变量值),将所需变量值如${token}设置为jmeter属性值,即newtoken,示例: 1.添加- ...

  10. 读 Zepto 源码之集合元素查找

    这篇依然是跟 dom 相关的方法,侧重点是跟集合元素查找相关的方法. 读Zepto源码系列文章已经放到了github上,欢迎star: reading-zepto 源码版本 本文阅读的源码为 zept ...