[LeetCode] 160. Intersection of Two Linked Lists

Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

For example, the following two linked lists begin to intersect at node c1:

The test cases are generated such that there are no cycles anywhere in the entire linked structure.

Note that the linked lists must retain their original structure after the function returns.

Custom Judge:

The inputs to the judge are given as follows (your program is not given these inputs):

  • intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
  • listA - The first linked list.
  • listB - The second linked list.
  • skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
  • skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.

The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.

 

Example 1:

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
Output: Intersected at '8'
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,6,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 = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Intersected at '2'
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 [1,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: No intersection
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.

Constraints:

  • The number of nodes of listA is in the m.
  • The number of nodes of listB is in the n.
  • 1 <= m, n <= 3 * 104
  • 1 <= Node.val <= 105
  • 0 <= skipA < m
  • 0 <= skipB < n
  • intersectVal is 0 if listA and listB do not intersect.
  • intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.

Follow up: Could you write a solution that runs in O(m + n) time and use only O(1) memory?

相交链表。

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

两种思路,一个是需要求出两个链表各自的长度,当两者不相等的时候,需要先遍历长的链表,使得其剩下的长度要跟短的链表长度相等,再去找两者的交点。

时间O(n)

空间O(1)

JavaScript实现

 1 /**
 2  * @param {ListNode} headA
 3  * @param {ListNode} headB
 4  * @return {ListNode}
 5  */
 6 var getIntersectionNode = function(headA, headB) {
 7     // corner case
 8     if (headA === null || headB === null) {
 9         return null;
10     }
11 
12     // normal case
13     let lenA = len(headA);
14     let lenB = len(headB);
15     if (lenA > lenB) {
16         while (lenA !== lenB) {
17             headA = headA.next;
18             lenA--;
19         }
20     } else {
21         while (lenA !== lenB) {
22             headB = headB.next;
23             lenB--;
24         }
25     }
26     while (headA !== headB) {
27         headA = headA.next;
28         headB = headB.next;
29     }
30     return headA;
31 };
32 
33 var len = function(head) {
34     let res = 1;
35     while (head !== null) {
36         res++;
37         head = head.next;
38     }
39     return res;
40 }

 

Java实现

 1 public class Solution {
 2     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
 3         // corner case
 4         if (headA == null || headB == null) {
 5             return null;
 6         }
 7 
 8         // normal case
 9         int lenA = len(headA);
10         int lenB = len(headB);
11         if (lenA > lenB) {
12             while (lenA != lenB) {
13                 headA = headA.next;
14                 lenA--;
15             }
16         } else {
17             while (lenA != lenB) {
18                 headB = headB.next;
19                 lenB--;
20             }
21         }
22         while (headA != headB) {
23             headA = headA.next;
24             headB = headB.next;
25         }
26         return headA;
27     }
28 
29     private int len(ListNode head) {
30         int len = 1;
31         while (head != null) {
32             head = head.next;
33             len++;
34         }
35         return len;
36     }
37 }

 

另外一种思路是不求两个链表的长度,分别遍历 A 和 B。如果还是按照第一个例子的话,遍历完 A 和 B 的时候并不能找到两者的交点,此时可以将 A 的末尾接上 B(A+B),或者将 B 的末尾接上 A(B+A),这样保证了两者遍历的长度相等,就一定能找到交点。这个例子给的不是特别好,因为遍历的时候,程序很可能在 8 之前的那个 1 的 node 就退出循环了。但是如果这个节点不相等,程序会在 8 的地方退出。

A: 4 - 1 - 8 - 4 - 5 - 5 - 0 - 1 - 8 - 4 - 5

B: 5 - 0 - 1 - 8 - 4 - 5 - 4 - 1 - 8 - 4 - 5

时间O(m + n), A和B的长度和

空间O(1)

JavaScript实现

 1 /**
 2  * @param {ListNode} headA
 3  * @param {ListNode} headB
 4  * @return {ListNode}
 5  */
 6 var getIntersectionNode = function(headA, headB) {
 7     // corner case
 8     if (headA === null || headB === null) {
 9         return null;;
10     }
11 
12     // normal case
13     let a = headA;
14     let b = headB;
15     while (a !== b) {
16         a = a === null ? headB : a.next;
17         b = b === null ? headA : b.next;
18     }
19     return a;
20 };

 

Java实现

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14         if (headA == null || headB == null) return null;
15         ListNode a = headA;
16         ListNode b = headB;
17         while (a != b) {
18             a = a == null ? headB : a.next;
19             b = b == null ? headA : b.next;
20         }
21         return a;
22     }
23 }

 

LeetCode 题目总结

posted @ 2019-11-10 07:54  CNoodle  阅读(199)  评论(0编辑  收藏  举报