Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98a7989

Browse files
committed
160 added
1 parent 52f0546 commit 98a7989

File tree

8 files changed

+154
-14
lines changed

8 files changed

+154
-14
lines changed
Loading
Loading
Loading
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [160. Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/)
2+
3+
Write a program to find the node at which the intersection of two singly linked lists begins.
4+
5+
For example, the following two linked lists:
6+
7+
![p](p.png)
8+
9+
begin to intersect at node c1.
10+
11+
Example 1:
12+
13+
![1](1.png)
14+
15+
```text
16+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
17+
Output: Reference of the node with value = 8
18+
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.
19+
```
20+
21+
Example 2:
22+
23+
![2](2.png)
24+
25+
```text
26+
Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
27+
Output: Reference of the node with value = 2
28+
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.
29+
```
30+
31+
Example 3:
32+
33+
![3](3.png)
34+
35+
```text
36+
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
37+
Output: null
38+
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.
39+
Explanation: The two lists do not intersect, so return null.
40+
```
41+
42+
Notes:
43+
44+
- If the two linked lists have no intersection at all, return null.
45+
- The linked lists must retain their original structure after the function returns.
46+
- You may assume there are no cycles anywhere in the entire linked structure.
47+
- Your code should preferably run in O(n) time and use only O(1) memory.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package problem0160
2+
3+
import "github.com/aQuaYi/LeetCode-in-Go/kit"
4+
5+
// ListNode is pre-defined...
6+
type ListNode = kit.ListNode
7+
8+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
9+
return nil
10+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package problem0160
2+
3+
import (
4+
"testing"
5+
6+
"github.com/aQuaYi/LeetCode-in-Go/kit"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// tcs is testcase slice
12+
var tcs = []struct {
13+
a, b []int
14+
ea, eb int
15+
}{
16+
17+
{
18+
[]int{4, 1, 8, 4, 5},
19+
[]int{5, 0, 1, 8, 4, 5},
20+
2,
21+
3,
22+
},
23+
24+
{
25+
[]int{0, 9, 1, 2, 4},
26+
[]int{3, 2, 4},
27+
3,
28+
1,
29+
},
30+
31+
{
32+
[]int{2, 6, 4},
33+
[]int{1, 5},
34+
3,
35+
2,
36+
},
37+
38+
// 可以有多个 testcase
39+
}
40+
41+
// head must Not be nil
42+
func tailOf(head *ListNode) *ListNode {
43+
for head.Next != nil {
44+
head = head.Next
45+
}
46+
return head
47+
}
48+
49+
func Test_getIntersectionNode(t *testing.T) {
50+
ast := assert.New(t)
51+
52+
for _, tc := range tcs {
53+
tail := kit.Ints2List(tc.a[tc.ea:])
54+
55+
headA := kit.Ints2List(tc.a[:tc.ea])
56+
tailA := tailOf(headA)
57+
tailA.Next = tail
58+
59+
headB := kit.Ints2List(tc.b[:tc.eb])
60+
tailB := tailOf(headB)
61+
tailB.Next = tail
62+
63+
ast.Equal(tail, getIntersectionNode(headA, headB), "输入:%v", tc)
64+
}
65+
}
66+
67+
func Benchmark_getIntersectionNode(b *testing.B) {
68+
for i := 0; i < b.N; i++ {
69+
for _, tc := range tcs {
70+
tail := kit.Ints2List(tc.a[tc.ea:])
71+
72+
headA := kit.Ints2List(tc.a[:tc.ea])
73+
tailA := tailOf(headA)
74+
tailA.Next = tail
75+
76+
headB := kit.Ints2List(tc.b[:tc.eb])
77+
tailB := tailOf(headB)
78+
tailB.Next = tail
79+
80+
getIntersectionNode(headA, headB)
81+
}
82+
}
83+
}
Loading

leetcode.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 873,
4-
"Updated": "2019-04-15T21:58:56.384624711+08:00",
3+
"Ranking": 863,
4+
"Updated": "2019-04-16T19:39:08.862122497+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 219,
@@ -181,7 +181,7 @@
181181
"ID": 13,
182182
"Title": "Roman to Integer",
183183
"TitleSlug": "roman-to-integer",
184-
"PassRate": "51%",
184+
"PassRate": "52%",
185185
"Difficulty": "Easy",
186186
"IsAccepted": true,
187187
"IsPaid": false,
@@ -961,7 +961,7 @@
961961
"ID": 78,
962962
"Title": "Subsets",
963963
"TitleSlug": "subsets",
964-
"PassRate": "51%",
964+
"PassRate": "52%",
965965
"Difficulty": "Medium",
966966
"IsAccepted": true,
967967
"IsPaid": false,
@@ -5953,7 +5953,7 @@
59535953
"ID": 494,
59545954
"Title": "Target Sum",
59555955
"TitleSlug": "target-sum",
5956-
"PassRate": "44%",
5956+
"PassRate": "45%",
59575957
"Difficulty": "Medium",
59585958
"IsAccepted": true,
59595959
"IsPaid": false,
@@ -8185,7 +8185,7 @@
81858185
"ID": 680,
81868186
"Title": "Valid Palindrome II",
81878187
"TitleSlug": "valid-palindrome-ii",
8188-
"PassRate": "33%",
8188+
"PassRate": "34%",
81898189
"Difficulty": "Easy",
81908190
"IsAccepted": true,
81918191
"IsPaid": false,
@@ -9949,7 +9949,7 @@
99499949
"ID": 827,
99509950
"Title": "Making A Large Island",
99519951
"TitleSlug": "making-a-large-island",
9952-
"PassRate": "42%",
9952+
"PassRate": "43%",
99539953
"Difficulty": "Hard",
99549954
"IsAccepted": true,
99559955
"IsPaid": false,
@@ -11137,7 +11137,7 @@
1113711137
"ID": 926,
1113811138
"Title": "Flip String to Monotone Increasing",
1113911139
"TitleSlug": "flip-string-to-monotone-increasing",
11140-
"PassRate": "48%",
11140+
"PassRate": "49%",
1114111141
"Difficulty": "Medium",
1114211142
"IsAccepted": true,
1114311143
"IsPaid": false,
@@ -11389,7 +11389,7 @@
1138911389
"ID": 947,
1139011390
"Title": "Most Stones Removed with Same Row or Column",
1139111391
"TitleSlug": "most-stones-removed-with-same-row-or-column",
11392-
"PassRate": "53%",
11392+
"PassRate": "54%",
1139311393
"Difficulty": "Medium",
1139411394
"IsAccepted": true,
1139511395
"IsPaid": false,
@@ -11737,7 +11737,7 @@
1173711737
"ID": 976,
1173811738
"Title": "Largest Perimeter Triangle",
1173911739
"TitleSlug": "largest-perimeter-triangle",
11740-
"PassRate": "56%",
11740+
"PassRate": "57%",
1174111741
"Difficulty": "Easy",
1174211742
"IsAccepted": false,
1174311743
"IsPaid": false,
@@ -12277,7 +12277,7 @@
1227712277
"ID": 1021,
1227812278
"Title": "Remove Outermost Parentheses",
1227912279
"TitleSlug": "remove-outermost-parentheses",
12280-
"PassRate": "80%",
12280+
"PassRate": "79%",
1228112281
"Difficulty": "Easy",
1228212282
"IsAccepted": false,
1228312283
"IsPaid": false,
@@ -12289,7 +12289,7 @@
1228912289
"ID": 1022,
1229012290
"Title": "Sum of Root To Leaf Binary Numbers",
1229112291
"TitleSlug": "sum-of-root-to-leaf-binary-numbers",
12292-
"PassRate": "45%",
12292+
"PassRate": "46%",
1229312293
"Difficulty": "Easy",
1229412294
"IsAccepted": false,
1229512295
"IsPaid": false,
@@ -12337,7 +12337,7 @@
1233712337
"ID": 1026,
1233812338
"Title": "Maximum Difference Between Node and Ancestor",
1233912339
"TitleSlug": "maximum-difference-between-node-and-ancestor",
12340-
"PassRate": "55%",
12340+
"PassRate": "56%",
1234112341
"Difficulty": "Medium",
1234212342
"IsAccepted": false,
1234312343
"IsPaid": false,
@@ -12349,7 +12349,7 @@
1234912349
"ID": 1027,
1235012350
"Title": "Longest Arithmetic Sequence",
1235112351
"TitleSlug": "longest-arithmetic-sequence",
12352-
"PassRate": "41%",
12352+
"PassRate": "43%",
1235312353
"Difficulty": "Medium",
1235412354
"IsAccepted": false,
1235512355
"IsPaid": false,

0 commit comments

Comments
 (0)