File tree 8 files changed +236
-23
lines changed
0142.Linked List Cycle II
8 files changed +236
-23
lines changed Original file line number Diff line number Diff line change 48
48
## 解法
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
51
+ 定义快慢指针 ` slow ` 、` fast ` ,初始指向 ` head ` 。
52
+
53
+ 快指针每次走两步,慢指针每次走一步,不断循环。当相遇时,说明链表存在环。如果循环结束依然没有相遇,说明链表不存在环。
54
+
51
55
52
56
<!-- tabs:start -->
53
57
54
58
### ** Python3**
55
59
<!-- 这里可写当前语言的特殊实现逻辑 -->
56
60
57
61
``` 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
59
76
```
60
77
61
78
### ** Java**
62
79
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
80
64
81
``` 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
+ }
66
107
```
67
108
68
109
### ** ...**
Original file line number Diff line number Diff line change 108
108
### ** Python3**
109
109
110
110
``` 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
112
125
```
113
126
114
127
### ** Java**
115
128
116
129
``` 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
+ }
118
155
```
119
156
120
157
### ** ...**
Original file line number Diff line number Diff line change @@ -8,8 +8,7 @@ class Solution:
8
8
def hasCycle (self , head : ListNode ) -> bool :
9
9
slow = fast = head
10
10
while fast and fast .next :
11
- slow = slow .next
12
- fast = fast .next .next
11
+ slow , fast = slow .next , fast .next .next
13
12
if slow == fast :
14
13
return True
15
14
return False
Original file line number Diff line number Diff line change 49
49
## 解法
50
50
<!-- 这里可写通用的实现逻辑 -->
51
51
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 ` 相遇。
52
65
53
66
<!-- tabs:start -->
54
67
55
68
### ** Python3**
56
69
<!-- 这里可写当前语言的特殊实现逻辑 -->
57
70
58
71
``` 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
60
93
```
61
94
62
95
### ** Java**
63
96
<!-- 这里可写当前语言的特殊实现逻辑 -->
64
97
65
98
``` 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
+ }
67
133
```
68
134
69
135
### ** ...**
Original file line number Diff line number Diff line change @@ -56,13 +56,66 @@ Can you solve it without using extra space?</p>
56
56
### ** Python3**
57
57
58
58
``` 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
60
80
```
61
81
62
82
### ** Java**
63
83
64
84
``` 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
+ }
66
119
```
67
120
68
121
### ** ...**
Original file line number Diff line number Diff line change 11
11
*/
12
12
public class Solution {
13
13
public ListNode detectCycle (ListNode head ) {
14
- ListNode slow = head ;
15
- ListNode fast = head ;
14
+ ListNode slow = head , fast = head ;
16
15
boolean hasCycle = false ;
17
16
while (fast != null && fast .next != null ) {
18
17
slow = slow .next ;
@@ -22,17 +21,14 @@ public ListNode detectCycle(ListNode head) {
22
21
break ;
23
22
}
24
23
}
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 ;
34
31
}
35
- return null ;
36
-
32
+ return p ;
37
33
}
38
34
}
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments