LeetCode_160. Intersection of Two Linked Lists
160. 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.
package leetcode.easy; /**
* Definition for singly-linked list. public class ListNode { int val; ListNode
* next; ListNode(int x) { val = x; next = null; } }
*/
public class IntersectionOfTwoLinkedLists {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (null == headA || null == headB) {
return null;
} else {
ListNode pA = headA;
ListNode pB = headB;
while (pA != pB) {
if (pA != null) {
pA = pA.next;
} else {
pA = headB;
}
if (pB != null) {
pB = pB.next;
} else {
pB = headA;
}
}
return pA;
}
} @org.junit.Test
public void test0() {
ListNode a1 = new ListNode(1);
ListNode a2 = new ListNode(2);
ListNode b1 = new ListNode(1);
ListNode b2 = new ListNode(2);
ListNode b3 = new ListNode(3);
ListNode c1 = new ListNode(1);
ListNode c2 = new ListNode(2);
ListNode c3 = new ListNode(3);
a1.next = a2;
b1.next = b2;
b2.next = b3;
a2.next = c1;
b3.next = c1;
c1.next = c2;
c2.next = c3;
c3.next = null;
System.out.println(getIntersectionNode(a1, b1).val);
} @org.junit.Test
public void test1() {
ListNode a1 = new ListNode(4);
ListNode a2 = new ListNode(1);
ListNode b1 = new ListNode(5);
ListNode b2 = new ListNode(0);
ListNode b3 = new ListNode(1);
ListNode c1 = new ListNode(8);
ListNode c2 = new ListNode(4);
ListNode c3 = new ListNode(5);
a1.next = a2;
b1.next = b2;
b2.next = b3;
a2.next = c1;
b3.next = c1;
c1.next = c2;
c2.next = c3;
c3.next = null;
System.out.println(getIntersectionNode(a1, b1).val);
} @org.junit.Test
public void test2() {
ListNode a1 = new ListNode(0);
ListNode a2 = new ListNode(9);
ListNode a3 = new ListNode(1);
ListNode b1 = new ListNode(3);
ListNode c1 = new ListNode(2);
ListNode c2 = new ListNode(4);
a1.next = a2;
a2.next = a3;
a3.next = c1;
b1.next = c1;
c1.next = c2;
c2.next = null;
System.out.println(getIntersectionNode(a1, b1).val);
} @org.junit.Test
public void test3() {
ListNode a1 = new ListNode(2);
ListNode a2 = new ListNode(6);
ListNode a3 = new ListNode(4);
ListNode b1 = new ListNode(1);
ListNode b2 = new ListNode(5);
a1.next = a2;
a2.next = a3;
a3.next = null;
b1.next = b2;
b2.next = null;
System.out.println(getIntersectionNode(a1, b1));
}
}
LeetCode_160. Intersection of Two Linked Lists的更多相关文章
- [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]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 ...
- 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--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- [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. ...
随机推荐
- P2458 [SDOI2006]保安站岗[树形dp]
题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...
- webpack中配置babel时遇到的问题
1.Error: Cannot find module '@babel/core' babel-loader@8 requires Babel 7.x (the package '@babel/cor ...
- P2P system: GNUTELLA
P2P system: GNUTELLA GNUTELLA是第一个经论证的分布式的peer-to-peer system. Napster的一个重大问题是涉及到间接侵权,所以GNUTELLA消除the ...
- UML类图介绍
一.概述 UML类图用来定义系统中的类,包括描述类的结构和类之间的关系.类图的主要作用于描述系统的静态结构. 类图的基本模型元素如下:
- Python 高级
GIL面试题如下 描述Python GIL的概念, 以及它对python多线程的影响?编写一个多线程抓取网页的程序,并阐明多线程抓取程序是否可比单线程性能有提升,并解释原因. Guido的声明:ht ...
- 洛谷P1050 循环【java大数】
题目:https://www.luogu.org/problemnew/show/P1050 题意:给定一个数$n$,问$n$的幂次的最低$k$位的循环节是多少. 思路:这真是我做过最难的java大数 ...
- Java多线程下载初试
一.服务端/客户端代码的实现 服务端配置config @ConfigurationProperties("storage") public class StoragePropert ...
- PHP 连接MySQL
连接MySQL 在我们访问MySQL数据库前,我们需要先连接到数据库服务器: 实例(MySQLi - 面向对象) <?php $servername = "localhost" ...
- html表格按长度换行
<table style="table-layout:fixed; WORD-BREAK: break-all; WORD-WRAP: break-word; width:200px; ...
- k8s aliyun mirros
安装kubernetes的时候,需要安装kubelet, kubeadm等包,但k8s官网给的yum源是packages.cloud.google.com,国内访问不了,此时我们可以使用阿里云的yum ...