-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathNo160.intersection-of-two-linked-lists.js
91 lines (83 loc) · 2.48 KB
/
No160.intersection-of-two-linked-lists.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Difficulty:
* Easy
*
* Desc:
* Write a program to find the node at which the intersection of two singly linked lists begins.
*
* Example:
* the following two linked lists:
* A: a1 → a2
↘
c1 → c2 → c3
↗
* B: b1 → b2 → b3
* begin to intersect at node c1.
*
* 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.
*
* 给定两个(单向)链表,判定它们是否相交并返回交点。
* 请注意相交的定义基于节点的引用,而不是基于节点的值。
* 换句话说,如果一个链表的第k个节点与另一个链表的第j个节点是同一节点(引用完全相同),则这两个链表相交。
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode_1 = function(headA, headB) {
const set = new Set()
while (headA || headB) {
if (headA) {
if (set.has(headA)) return headA
set.add(headA)
headA = headA.next
}
if (headB) {
if (set.has(headB)) return headB
set.add(headB)
headB = headB.next
}
}
return null
}
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*
* 思路:
* A、B 链接无所谓各自长度多少,只需两个指针 nodeA 和 nodeB,以一样的速度,走 A + B 的距离,则会同时在终点处结束。
* 如果 A、B 有交点,则因为在交点后距离一直,则两指针会在交点处相遇;
* 否则,两指针只能在链表末端相遇
* https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/xiang-jiao-lian-biao-by-leetcode/
*/
var getIntersectionNode_2 = function(headA, headB) {
if (!headA || !headB) return null
let nodeA = headA
let nodeB = headB
while (nodeA || nodeB) {
if (nodeA === nodeB) return nodeA
if (!nodeA.next && !nodeB.next) return null
nodeA = nodeA.next ? nodeA.next : headB
nodeB = nodeB.next ? nodeB.next : headA
}
}