File tree 3 files changed +66
-0
lines changed
3 files changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ # linked-list-cycle(判断链表中是否存在环)
2
+
3
+ <center >知识点:链表</center >
4
+
5
+
6
+ ## 题目描述
7
+
8
+ Given a linked list, determine if it has a cycle in it.
9
+
10
+ Follow up:
11
+ Can you solve it without using extra space?
12
+
13
+ 给定一个链表,判断其中是否存在环。
14
+
15
+ 额外要求:
16
+ 尽量不要使用额外空间
17
+
18
+
19
+ ## 解题思路
20
+
21
+ 使用快慢指针即可。
22
+
23
+ ## 代码
24
+
25
+ [ 这里] ( ../src/ten/Solution.java )
Original file line number Diff line number Diff line change
1
+ package ten ;
2
+
3
+
4
+ /**
5
+ * @author dmrfcoder
6
+ * @date 2019/4/10
7
+ */
8
+
9
+ class ListNode {
10
+ int val ;
11
+ ListNode next ;
12
+
13
+ ListNode (int x ) {
14
+ val = x ;
15
+ next = null ;
16
+ }
17
+ }
18
+
19
+ public class Solution {
20
+
21
+ public boolean hasCycle (ListNode head ) {
22
+
23
+ if (head == null ) {
24
+ return false ;
25
+ }
26
+ ListNode fast , slow ;
27
+ fast = slow = head ;
28
+
29
+ while (fast .next != null && fast .next .next != null ) {
30
+ fast = fast .next .next ;
31
+ slow = slow .next ;
32
+ if (fast == slow ) {
33
+ return true ;
34
+ }
35
+ }
36
+ return false ;
37
+
38
+
39
+ }
40
+ }
Original file line number Diff line number Diff line change 151
151
- [ binary-tree-preorder-traversal(先序遍历二叉树)] ( ./LeetCode/Doc/先序遍历二叉树.md )
152
152
- [ reorder-list(链表重排序)] ( ./LeetCode/Doc/链表重排序.md )
153
153
- [ linked-list-cycle-ii(找出链表中环的入口节点)] ( ./LeetCode/Doc/找出链表中环的入口节点.md )
154
+ - [ linked-list-cycle(判断链表中是否存在环)] ( ./LeetCode/Doc/判断链表中是否存在环.md )
154
155
You can’t perform that action at this time.
0 commit comments