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

  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--LinkedList--160. Intersection of Two Linked Lists(Easy)

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

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

  9. 【leetcode】Intersection of Two Linked Lists

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

随机推荐

  1. ARRAY_MAP函数用法

    ARRAY_MAP函数用法 Posted on 2012-9-18, 22:15, by tmser, under php 总结 . 看php JSON 类中有这样一种用法array_map(arra ...

  2. 网络编程---scoket使用,七层协议,三次挥手建连接,四次挥手断连接

    目录 == 网络编程 == 软件开发架构 网络编程 互联网协议 TCP协议的工作原理 Socket == 网络编程 == 软件开发架构 开发软件 必须要开发一套 客户端与服务端 客户端与服务端的作用 ...

  3. 解决appium自带的Chromedriver版本和设备Android System Webview版本不一致的问题

    报错信息 selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred w ...

  4. js实现图片上传实时显示

    在开发的时候经常遇到这样的需求,用户在上传图片的时候,想要看到自己上传的图片是否正确,这时候需要把用户上传的图片及时显示出来,然后等他点击上传的时候,程序再执行上传到服务器. <!DOCTYPE ...

  5. sql server newid() 的用法

    sql newid()随机函数   从A表随机取2条记录,用SELECT TOP 10 * FROM ywle order by newid()order by 一般是根据某一字段排序,newid() ...

  6. Oracle 10g和11g中的自动统计任务

    1)  先来看下oracle 10g中的自动统计任务的问题. 从Oracle Database 10g开始,Oracle在建库后就默认创建了一个名为GATHER_STATS_JOB的定时任务,用于自动 ...

  7. web+页面支持批量下载吗

    一.此方法火狐有些版本是不支持的 window.location.href = 'https://*****.oss-cn-**.aliyuncs.com/*********';二.为了解决火狐有些版 ...

  8. 【テンプレート】字符串hash

    不懂hash是什么的盆友给出直通车:滴滴滴,开车啦~ 如果你看懂了的话: hash模板来也~ #include <cstdio> #include <string> #incl ...

  9. 实现返回顶部-wepy小程序-前端梳理

    <script type="text/javascript" src="http://hovertree.com/ziyuan/jquery/jquery-1.11 ...

  10. Python豆瓣书籍信息爬虫

    练习下BeautifulSoup,requests库,用python3.3 写了一个简易的豆瓣小爬虫,将爬取的信息在控制台输出并且写入文件中. 上源码: # coding = utf-8 '''my ...