Skip to content

Commit b57df29

Browse files
committed
feat: add python and java solutions to leetcode problem: No.142
更新LeetCode题解:142. 环形链表 II
1 parent e39eb75 commit b57df29

File tree

8 files changed

+236
-23
lines changed

8 files changed

+236
-23
lines changed

solution/0100-0199/0141.Linked List Cycle/README.md

+43-2
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,62 @@
4848
## 解法
4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
定义快慢指针 `slow``fast`,初始指向 `head`
52+
53+
快指针每次走两步,慢指针每次走一步,不断循环。当相遇时,说明链表存在环。如果循环结束依然没有相遇,说明链表不存在环。
54+
5155

5256
<!-- tabs:start -->
5357

5458
### **Python3**
5559
<!-- 这里可写当前语言的特殊实现逻辑 -->
5660

5761
```python
58-
62+
# Definition for singly-linked list.
63+
# class ListNode:
64+
# def __init__(self, x):
65+
# self.val = x
66+
# self.next = None
67+
68+
class Solution:
69+
def hasCycle(self, head: ListNode) -> bool:
70+
slow = fast = head
71+
while fast and fast.next:
72+
slow, fast = slow.next, fast.next.next
73+
if slow == fast:
74+
return True
75+
return False
5976
```
6077

6178
### **Java**
6279
<!-- 这里可写当前语言的特殊实现逻辑 -->
6380

6481
```java
65-
82+
/**
83+
* Definition for singly-linked list.
84+
* class ListNode {
85+
* int val;
86+
* ListNode next;
87+
* ListNode(int x) {
88+
* val = x;
89+
* next = null;
90+
* }
91+
* }
92+
*/
93+
public class Solution {
94+
public boolean hasCycle(ListNode head) {
95+
ListNode slow = head;
96+
ListNode fast = head;
97+
while (fast != null && fast.next != null) {
98+
slow = slow.next;
99+
fast = fast.next.next;
100+
if (slow == fast) {
101+
return true;
102+
}
103+
}
104+
return false;
105+
}
106+
}
66107
```
67108

68109
### **...**

solution/0100-0199/0141.Linked List Cycle/README_EN.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,50 @@
108108
### **Python3**
109109

110110
```python
111-
111+
# Definition for singly-linked list.
112+
# class ListNode:
113+
# def __init__(self, x):
114+
# self.val = x
115+
# self.next = None
116+
117+
class Solution:
118+
def hasCycle(self, head: ListNode) -> bool:
119+
slow = fast = head
120+
while fast and fast.next:
121+
slow, fast = slow.next, fast.next.next
122+
if slow == fast:
123+
return True
124+
return False
112125
```
113126

114127
### **Java**
115128

116129
```java
117-
130+
/**
131+
* Definition for singly-linked list.
132+
* class ListNode {
133+
* int val;
134+
* ListNode next;
135+
* ListNode(int x) {
136+
* val = x;
137+
* next = null;
138+
* }
139+
* }
140+
*/
141+
public class Solution {
142+
public boolean hasCycle(ListNode head) {
143+
ListNode slow = head;
144+
ListNode fast = head;
145+
while (fast != null && fast.next != null) {
146+
slow = slow.next;
147+
fast = fast.next.next;
148+
if (slow == fast) {
149+
return true;
150+
}
151+
}
152+
return false;
153+
}
154+
}
118155
```
119156

120157
### **...**

solution/0100-0199/0141.Linked List Cycle/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ class Solution:
88
def hasCycle(self, head: ListNode) -> bool:
99
slow = fast = head
1010
while fast and fast.next:
11-
slow = slow.next
12-
fast = fast.next.next
11+
slow, fast = slow.next, fast.next.next
1312
if slow == fast:
1413
return True
1514
return False

solution/0100-0199/0142.Linked List Cycle II/README.md

+68-2
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,87 @@
4949
## 解法
5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
先利用快慢指针判断链表是否有环,没有环则直接返回 `null`
53+
54+
若链表有环,我们分析快慢相遇时走过的距离。
55+
56+
对于慢指针,走过的距离为 `S=X+Y` ①;快指针走过的距离为 `2S=X+Y+N(Y+Z)` ②。如下图所示,其中 `N` 表示快指针与慢指针相遇时在环中所走过的圈数,而我们要求的环入口,也即是 `X` 的距离:
57+
58+
![](./images/linked-list-cycle-ii.png)
59+
60+
我们根据式子①②,得出 `X+Y=N(Y+Z)` => `X=(N-1)(Y+Z)+Z`
61+
62+
`N=1`(快指针在环中走了一圈与慢指针相遇) 时,`X=(1-1)(Y+Z)+Z`,即 `X=Z`。此时只要定义一个 `p` 指针指向头节点,然后慢指针与 `p` 开始同时走,当慢指针与 `p` 相遇时,也就到达了环入口,直接返回 `p` 即可。
63+
64+
`N>1`时,也是同样的,说明慢指针除了走 `Z` 步,还需要绕 `N-1` 圈才能与 `p` 相遇。
5265

5366
<!-- tabs:start -->
5467

5568
### **Python3**
5669
<!-- 这里可写当前语言的特殊实现逻辑 -->
5770

5871
```python
59-
72+
# Definition for singly-linked list.
73+
# class ListNode:
74+
# def __init__(self, x):
75+
# self.val = x
76+
# self.next = None
77+
78+
class Solution:
79+
def detectCycle(self, head: ListNode) -> ListNode:
80+
slow = fast = head
81+
has_cycle = False
82+
while fast and fast.next:
83+
slow, fast = slow.next, fast.next.next
84+
if slow == fast:
85+
has_cycle = True
86+
break
87+
if not has_cycle:
88+
return None
89+
p = head
90+
while p != slow:
91+
p, slow = p.next, slow.next
92+
return p
6093
```
6194

6295
### **Java**
6396
<!-- 这里可写当前语言的特殊实现逻辑 -->
6497

6598
```java
66-
99+
/**
100+
* Definition for singly-linked list.
101+
* class ListNode {
102+
* int val;
103+
* ListNode next;
104+
* ListNode(int x) {
105+
* val = x;
106+
* next = null;
107+
* }
108+
* }
109+
*/
110+
public class Solution {
111+
public ListNode detectCycle(ListNode head) {
112+
ListNode slow = head, fast = head;
113+
boolean hasCycle = false;
114+
while (fast != null && fast.next != null) {
115+
slow = slow.next;
116+
fast = fast.next.next;
117+
if (slow == fast) {
118+
hasCycle = true;
119+
break;
120+
}
121+
}
122+
if (!hasCycle) {
123+
return null;
124+
}
125+
ListNode p = head;
126+
while (p != slow) {
127+
p = p.next;
128+
slow = slow.next;
129+
}
130+
return p;
131+
}
132+
}
67133
```
68134

69135
### **...**

solution/0100-0199/0142.Linked List Cycle II/README_EN.md

+55-2
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,66 @@ Can you solve it without using extra space?</p>
5656
### **Python3**
5757

5858
```python
59-
59+
# Definition for singly-linked list.
60+
# class ListNode:
61+
# def __init__(self, x):
62+
# self.val = x
63+
# self.next = None
64+
65+
class Solution:
66+
def detectCycle(self, head: ListNode) -> ListNode:
67+
slow = fast = head
68+
has_cycle = False
69+
while fast and fast.next:
70+
slow, fast = slow.next, fast.next.next
71+
if slow == fast:
72+
has_cycle = True
73+
break
74+
if not has_cycle:
75+
return None
76+
p = head
77+
while p != slow:
78+
p, slow = p.next, slow.next
79+
return p
6080
```
6181

6282
### **Java**
6383

6484
```java
65-
85+
/**
86+
* Definition for singly-linked list.
87+
* class ListNode {
88+
* int val;
89+
* ListNode next;
90+
* ListNode(int x) {
91+
* val = x;
92+
* next = null;
93+
* }
94+
* }
95+
*/
96+
public class Solution {
97+
public ListNode detectCycle(ListNode head) {
98+
ListNode slow = head, fast = head;
99+
boolean hasCycle = false;
100+
while (fast != null && fast.next != null) {
101+
slow = slow.next;
102+
fast = fast.next.next;
103+
if (slow == fast) {
104+
hasCycle = true;
105+
break;
106+
}
107+
}
108+
if (!hasCycle) {
109+
return null;
110+
}
111+
ListNode p = head;
112+
while (p != slow) {
113+
p = p.next;
114+
slow = slow.next;
115+
}
116+
return p;
117+
}
118+
}
66119
```
67120

68121
### **...**

solution/0100-0199/0142.Linked List Cycle II/Solution.java

+9-13
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
*/
1212
public class Solution {
1313
public ListNode detectCycle(ListNode head) {
14-
ListNode slow = head;
15-
ListNode fast = head;
14+
ListNode slow = head, fast = head;
1615
boolean hasCycle = false;
1716
while (fast != null && fast.next != null) {
1817
slow = slow.next;
@@ -22,17 +21,14 @@ public ListNode detectCycle(ListNode head) {
2221
break;
2322
}
2423
}
25-
26-
if (hasCycle) {
27-
ListNode p1 = head;
28-
ListNode p2 = slow;
29-
while (p1 != p2) {
30-
p1 = p1.next;
31-
p2 = p2.next;
32-
}
33-
return p1;
24+
if (!hasCycle) {
25+
return null;
26+
}
27+
ListNode p = head;
28+
while (p != slow) {
29+
p = p.next;
30+
slow = slow.next;
3431
}
35-
return null;
36-
32+
return p;
3733
}
3834
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
slow = fast = head
10+
has_cycle = False
11+
while fast and fast.next:
12+
slow, fast = slow.next, fast.next.next
13+
if slow == fast:
14+
has_cycle = True
15+
break
16+
if not has_cycle:
17+
return None
18+
p = head
19+
while p != slow:
20+
p, slow = p.next, slow.next
21+
return p
Loading

0 commit comments

Comments
 (0)