160. Intersection of Two Linked Lists(Easy)

题目地址https://leetcode.com/problems/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:

begin to intersect at node c1.

Example 1:

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.

Example 2:

Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.

Example 3:

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.

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.

solution

题意是给定两个链表头headA与headB,判断两个链表是否有交叉的部分,若有,则返回交叉部分的起点结点,否则,返回null。

解法一

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p = headA, q = headB;
while (p != q) //如果两指针重合
{
p = (p != null) ? p.next : headB;
q = (q != null) ? q.next : headA;
}
return p;
}
}

解析:解法一的思路很巧妙,代码量也很少。虽然题目中强调了链表中不存在环,但是我们可以用环的思想来做,我们让两条链表分别从各自的开头开始往后遍历,当其中一条遍历到末尾时,我们跳到另一个条链表的开头继续遍历。两个指针最终会相等,而且只有两种情况。若headA的长度大于headB,则当遍历完headA时,headA的遍历指针p指向headB,当遍历完headB时,headB的遍历指针q指向headA,此时,p与q刚好指向headA与headB右对齐后的同一位置,即headA的头结点处。随后只需继续同时遍历两个链表,并比较对应位置的结点是否交叉。

解法二

public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p = headA, q = headB;
int alength = 0,blength = 0;
while (p != null)
{
alength++;
p = p.next;
}
while (q != null)
{
blength++;
q = q.next;
}
p = headA; q = headB; int i = 0, start = 0;
if (alength > blength)
{
start = alength - blength;
while (p != null && i < start)
{
p = p.next;
i++;
}
}
else
{
start = blength - alength;
while (q != null && i < start)
{
q = q.next;
i++;
}
}
while (p != null && q != null && p != q)
{
p = p.next;
q = q.next;
}
return p;
}
}

解析:解法二思路很直接,代码量也多点。如果两个链长度相同的话,那么对应的一个个比下去就能找到,否则只需要把长链表变短再挨个比较即可。具体做法为:分别遍历两个链表,得到其对应的长度。然后求长度的差值,把较长的那个链表向后移动这个差值的个数,然后一一比较即可。

reference

https://www.cnblogs.com/grandyang/p/4128461.html

Notes

1.链表问题思路较多,注意不同思路的差异;

LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)的更多相关文章

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

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

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

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

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

  4. 【LeetCode】160. Intersection of Two Linked Lists

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

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

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

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

  8. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

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

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

随机推荐

  1. Julia控制流

  2. ASE课程总结 by 冯晓云

    开始的开始,采访往届ASE班的blog:http://www.cnblogs.com/legs/p/4894362.html 和北航软工M1检查:http://www.cnblogs.com/legs ...

  3. 常用App用户体验找茬

    冯晓云: 哔哩哔哩手机客户端:视频播放只允许横屏全屏:还有长视频的“5分钟诅咒”,遇到网速不好的时候是个大写的悲剧: 必应词典UWP版本:主页新闻链接跳转后,一些页面不支持划词取译,当然本身各个页面也 ...

  4. 实验一 熟悉IDLE和在线编程平台

    实验目的 1.掌握python IDLE集成开发环境的安装与使用 2.熟悉在线编程平台 3.掌握基本的python程序编写.编译与运行程序的方法 实验内容 1.按照实验指导安装IDLE,尝试交互式运行 ...

  5. redis的安装(ubuntu版本)

    1.使用apt-get命令进行安装 安装gcc依赖 root@yatces-virtual-machine:~# apt-get update root@yatces-virtual-machine: ...

  6. PHP函数:array_key_exists

    array_key_exists()  - 检查数组里是否有指定的键名或索引. 注意:array_key_exists() 仅仅搜索第一维的键. 多维数组里嵌套的键不会被搜索到. 说明: rray_k ...

  7. cmd命令行中查看、修改、删除与添加环境变量

    注意:只在当前窗口生效!! 1.查看当前所有可用的环境变量:输入 set 即可查看. set 2.查看某个环境变量:输入 “set 变量名”即可 set python 3.修改环境变量 :输入 “se ...

  8. JDBC 进阶:使用封装通用DML DQL 和结构分层以及at com.mysql.jdbc.PreparedStatement.setTimestamp空指针异常解决

    准备: 数据表 CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(10) DEFAULT ...

  9. Go语言 2019 调查报告发布

    Go 官方博客昨日公布了[ 2019 年 Go 语言调查报告].本次调查收到的回复达到 10,975 份,约为去年的两倍. 这些受访者的反馈意见将被选取用于改进 Go 语言的发展. 以下是 2019 ...

  10. cdn服务器

    CDN的基本原理和基础架构 CDN是将源站内容分发至最接近用户的节点,使用户可就近取得所需内容,提高用户访问的响应速度和成功率.解决因分布.带宽.服务器性能带来的访问延迟问题,适用于站点加速.点播.直 ...