Skip to content

Commit 786cadc

Browse files
committedMar 19, 2020
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 02.08. 环路检测
1 parent 7de92ba commit 786cadc

File tree

4 files changed

+214
-50
lines changed

4 files changed

+214
-50
lines changed
 

‎lcci/02.08.Linked List Cycle/README.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,60 @@
1212
<!-- 这里可写当前语言的特殊实现逻辑 -->
1313

1414
```python
15+
# Definition for singly-linked list.
16+
# class ListNode:
17+
# def __init__(self, x):
18+
# self.val = x
19+
# self.next = None
1520

21+
class Solution:
22+
def detectCycle(self, head: ListNode) -> ListNode:
23+
fast = slow = p = head
24+
while fast and fast.next:
25+
fast = fast.next.next
26+
slow = slow.next
27+
if fast == slow:
28+
break
29+
if fast is None or fast.next is None:
30+
return None
31+
while slow != p:
32+
p = p.next
33+
slow = slow.next
34+
return p
1635
```
1736

1837
### Java
1938
<!-- 这里可写当前语言的特殊实现逻辑 -->
2039

2140
```java
22-
41+
/**
42+
* Definition for singly-linked list.
43+
* class ListNode {
44+
* int val;
45+
* ListNode next;
46+
* ListNode(int x) {
47+
* val = x;
48+
* next = null;
49+
* }
50+
* }
51+
*/
52+
public class Solution {
53+
public ListNode detectCycle(ListNode head) {
54+
ListNode fast = head, slow = head;
55+
while (fast != null && fast.next != null) {
56+
fast = fast.next.next;
57+
slow = slow.next;
58+
if (fast == slow) break;
59+
}
60+
if (fast == null || fast.next == null) return null;
61+
ListNode p = head;
62+
while (p != slow) {
63+
p = p.next;
64+
slow = slow.next;
65+
}
66+
return p;
67+
}
68+
}
2369
```
2470

2571
### ...
+119-49
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,119 @@
1-
# [02.08. Linked List Cycle](https://leetcode-cn.com/problems/linked-list-cycle-lcci)
2-
3-
## Description
4-
<p>Given a circular linked list, implement an algorithm that returns the node at the beginning of the loop.</p>
5-
6-
<p>Circular linked list: A (corrupt) linked list in which a node&#39;s next pointer points to an earlier node, so as to make a loop in the linked list.</p>
7-
8-
<p><strong>Example 1: </strong></p>
9-
10-
<pre>
11-
<strong>Input: </strong>head = [3,2,0,-4], pos = 1
12-
<strong>Output: </strong>tail connects to node index 1</pre>
13-
14-
<p><strong>Example 2: </strong></p>
15-
16-
<pre>
17-
<strong>Input: </strong>head = [1,2], pos = 0
18-
<strong>Output: </strong>tail connects to node index 0</pre>
19-
20-
<p><strong>Example 3: </strong></p>
21-
22-
<pre>
23-
<strong>Input: </strong>head = [1], pos = -1
24-
<strong>Output: </strong>no cycle</pre>
25-
26-
<p><strong>Follow Up: </strong><br />
27-
Can you solve it without using additional space?</p>
28-
29-
30-
31-
## Solutions
32-
33-
34-
### Python3
35-
36-
```python
37-
38-
```
39-
40-
### Java
41-
42-
```java
43-
44-
```
45-
46-
### ...
47-
```
48-
49-
```
1+
# [02.08. Linked List Cycle](https://leetcode-cn.com/problems/linked-list-cycle-lcci)
2+
3+
## Description
4+
<p>Given a circular linked list, implement an algorithm that returns the node at the beginning of the loop.</p>
5+
6+
7+
8+
<p>Circular linked list: A (corrupt) linked list in which a node&#39;s next pointer points to an earlier node, so as to make a loop in the linked list.</p>
9+
10+
11+
12+
<p><strong>Example 1: </strong></p>
13+
14+
15+
16+
<pre>
17+
18+
<strong>Input: </strong>head = [3,2,0,-4], pos = 1
19+
20+
<strong>Output: </strong>tail connects to node index 1</pre>
21+
22+
23+
24+
<p><strong>Example 2: </strong></p>
25+
26+
27+
28+
<pre>
29+
30+
<strong>Input: </strong>head = [1,2], pos = 0
31+
32+
<strong>Output: </strong>tail connects to node index 0</pre>
33+
34+
35+
36+
<p><strong>Example 3: </strong></p>
37+
38+
39+
40+
<pre>
41+
42+
<strong>Input: </strong>head = [1], pos = -1
43+
44+
<strong>Output: </strong>no cycle</pre>
45+
46+
47+
48+
<p><strong>Follow Up: </strong><br />
49+
50+
Can you solve it without using additional space?</p>
51+
52+
53+
54+
55+
## Solutions
56+
57+
58+
### Python3
59+
60+
```python
61+
# Definition for singly-linked list.
62+
# class ListNode:
63+
# def __init__(self, x):
64+
# self.val = x
65+
# self.next = None
66+
67+
class Solution:
68+
def detectCycle(self, head: ListNode) -> ListNode:
69+
fast = slow = p = head
70+
while fast and fast.next:
71+
fast = fast.next.next
72+
slow = slow.next
73+
if fast == slow:
74+
break
75+
if fast is None or fast.next is None:
76+
return None
77+
while slow != p:
78+
p = p.next
79+
slow = slow.next
80+
return p
81+
```
82+
83+
### Java
84+
85+
```java
86+
/**
87+
* Definition for singly-linked list.
88+
* class ListNode {
89+
* int val;
90+
* ListNode next;
91+
* ListNode(int x) {
92+
* val = x;
93+
* next = null;
94+
* }
95+
* }
96+
*/
97+
public class Solution {
98+
public ListNode detectCycle(ListNode head) {
99+
ListNode fast = head, slow = head;
100+
while (fast != null && fast.next != null) {
101+
fast = fast.next.next;
102+
slow = slow.next;
103+
if (fast == slow) break;
104+
}
105+
if (fast == null || fast.next == null) return null;
106+
ListNode p = head;
107+
while (p != slow) {
108+
p = p.next;
109+
slow = slow.next;
110+
}
111+
return p;
112+
}
113+
}
114+
```
115+
116+
### ...
117+
```
118+
119+
```
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* 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 detectCycle(ListNode head) {
14+
ListNode fast = head, slow = head;
15+
while (fast != null && fast.next != null) {
16+
fast = fast.next.next;
17+
slow = slow.next;
18+
if (fast == slow) break;
19+
}
20+
if (fast == null || fast.next == null) return null;
21+
ListNode p = head;
22+
while (p != slow) {
23+
p = p.next;
24+
slow = slow.next;
25+
}
26+
return p;
27+
}
28+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def detectCycle(self, head: ListNode) -> ListNode:
9+
fast = slow = p = head
10+
while fast and fast.next:
11+
fast = fast.next.next
12+
slow = slow.next
13+
if fast == slow:
14+
break
15+
if fast is None or fast.next is None:
16+
return None
17+
while slow != p:
18+
p = p.next
19+
slow = slow.next
20+
return p

0 commit comments

Comments
 (0)
Please sign in to comment.