Skip to content

Commit 73416cb

Browse files
committed
142 added
1 parent 89ff7c6 commit 73416cb

File tree

7 files changed

+103
-5
lines changed

7 files changed

+103
-5
lines changed
11 KB
Loading
4.47 KB
Loading
1.92 KB
Loading
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [142. Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)
2+
3+
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
4+
5+
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
6+
7+
Note: Do not modify the linked list.
8+
9+
Example 1:
10+
11+
```text
12+
Input: head = [3,2,0,-4], pos = 1
13+
Output: tail connects to node index 1
14+
Explanation: There is a cycle in the linked list, where tail connects to the second node.
15+
```
16+
17+
Example 2:
18+
19+
```text
20+
Input: head = [1,2], pos = 0
21+
Output: tail connects to node index 0
22+
Explanation: There is a cycle in the linked list, where tail connects to the first node.
23+
```
24+
25+
Example 3:
26+
27+
```text
28+
Input: head = [1], pos = -1
29+
Output: no cycle
30+
Explanation: There is no cycle in the linked list.
31+
```
32+
33+
Follow up:
34+
35+
- Can you solve it without using extra space?
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package problem0142
2+
3+
import "github.com/aQuaYi/LeetCode-in-Go/kit"
4+
5+
// ListNode is pre-defined...
6+
type ListNode = kit.ListNode
7+
8+
func detectCycle(head *ListNode) *ListNode {
9+
10+
return nil
11+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package problem0142
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+
ints []int
14+
pos int
15+
}{
16+
17+
{
18+
[]int{3, 2, 0, -4},
19+
1,
20+
},
21+
22+
{
23+
[]int{1, 2},
24+
0,
25+
},
26+
27+
{
28+
[]int{1},
29+
-1,
30+
},
31+
32+
// 可以有多个 testcase
33+
}
34+
35+
func Test_detectCycle(t *testing.T) {
36+
ast := assert.New(t)
37+
38+
for _, tc := range tcs {
39+
head := kit.Ints2ListWithCycle(tc.ints, tc.pos)
40+
ans := head.GetNodeWith(tc.ints[tc.pos])
41+
ast.Equal(ans, detectCycle(head), "输入:%v", tc)
42+
}
43+
}
44+
45+
func Benchmark_detectCycle(b *testing.B) {
46+
for i := 0; i < b.N; i++ {
47+
for _, tc := range tcs {
48+
head := kit.Ints2ListWithCycle(tc.ints, tc.pos)
49+
detectCycle(head)
50+
}
51+
}
52+
}

leetcode.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 873,
4-
"Updated": "2019-04-15T19:48:38.251156934+08:00",
4+
"Updated": "2019-04-15T20:27:07.119847203+08:00",
55
"Record": {
66
"Easy": {
7-
"Solved": 218,
7+
"Solved": 219,
88
"Total": 238
99
},
1010
"Medium": {
@@ -16,7 +16,7 @@
1616
"Total": 174
1717
},
1818
"Total": {
19-
"Solved": 755,
19+
"Solved": 756,
2020
"Total": 817
2121
}
2222
},
@@ -1719,7 +1719,7 @@
17191719
"TitleSlug": "linked-list-cycle",
17201720
"PassRate": "36%",
17211721
"Difficulty": "Easy",
1722-
"IsAccepted": false,
1722+
"IsAccepted": true,
17231723
"IsPaid": false,
17241724
"IsFavor": false,
17251725
"IsNew": 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,

0 commit comments

Comments
 (0)