LeetCode160 | Intersection-of-two-linked-lists
题目地址(160. 相交链表)
https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
题目描述
编写一个程序,找到两个单链表相交的起始节点。如下面的两个链表:在节点 c1 开始相交。示例 1:输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Reference of the node with value = 8
输入解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。示例 2:输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Reference of the node with value = 2
输入解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。示例 3:输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
解释:这两个链表不相交,因此返回 null。注意:如果两个链表没有交点,返回 null.
在返回结果后,两个链表仍须保持原有的结构。
可假定整个链表结构中没有循环。
程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
前置知识
- 本题中单链表ListNode数据结构包括一个整型的val,和一个指针next,这意味着对于给定的两条单链表要么无重复部分,要么有重复部分但绝对是“分-合”的状态,绝不会是“分-合-分”。
思路
- 对单链表A和B的值逐一对应比较,当A(这里假设A比较短)走到A的尾部后,若仍没有结束则转向B的头部从B开始;同理B(这里假设B比较长)走到B的尾部后,转向A的头部从A开始。
关键点
- 这样做其实表示l1与l2走过的距离是相同的,两者如果有intersect则必定最快得到结果
- A(a+c),B(b+c) ====>>>> a + c + b = b + c + a
代码
- 语言支持:Java
Java Code:
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/
public class Solution {// ListNode是函数的返回值,即对应节点的valuepublic ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode l1 = headA, l2 = headB;while (l1 != l2) {l1 = (l1 == null) ? headB : l1.next;l2 = (l2 == null) ? headA : l2.next;}return l1;}
}
复杂度分析
令 n 为数组长度。
- 时间复杂度:O(a+b)O(a+b)O(a+b)
- 空间复杂度:O(1)O(1)O(1)
LeetCode160 | Intersection-of-two-linked-lists相关推荐
- [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(160): Intersection of Two Linked Lists
Intersection of Two Linked Lists: Write a program to find the node at which the intersection of two ...
- [Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Intersection of Two Linked Lists——经典问题
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Intersection of Two Linked Lists 解答
Question Write a program to find the node at which the intersection of two singly linked lists begin ...
- 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. For ex ...
- [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 ...
- 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从零单排】No.160 Intersection of Two Linked Lists
题目 Write a program to find the node at which the intersection of two singly linked lists begins. For ...
最新文章
- 请教个问题,ajax.net 的效率如何
- 【设计模式】外观模式 ( 概念 | 适用场景 | 优缺点 | 代码示例 )
- Android CameraSurfaceView在SurfaceView上实现拍照,视频录像
- postman 抓包工具charles的使用
- SharpStrike:基于C#实现的后渗透漏洞利用工具
- oracle rac alter日志,ORACLE 11G RAC 增加日志组及增大日志文件
- VBA SQL查询-记录集转数组
- linux 嵌入式串口通信,基于linux的嵌入式串口通信综述.doc
- Xshell官网免费版
- 房东要涨800房租,我用Python抓取帝都几万套房源信息,主动涨了1000。
- 英文论文评审意见_英文论文审稿意见模板
- xcel Home 数据透视表初级班(10118班)小结第二课时
- TO_DATE()函数?
- 08——驾校科目一考试——布局按钮
- MMX指令集系列之二----移位运算和乘法指令
- Cannot access ‘router‘ before initialization
- 韦东山:驱动和APP,根本不应该上升到互相鄙视的地步
- HTML 框架 noresize=noresize 作用
- 如何解除网页禁止粘贴复制的情况?
- c语言拍飞虫课程设计报告,c语言课程设计_拍飞虫提高篇.doc