Skip to content

Commit ea58255

Browse files
committed
feat: add the solution of Linked List Cycle(141) with java.
1 parent 82e88fd commit ea58255

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
| [122][122-question] | [Best Time to Buy and Sell Stock II][122-tips] | [Easy][E] | [][122-java] | | [][122-kotlin] |
8888
| [125][125-question] | [Valid Palindrome][125-tips] | [Easy][E] | | | [][125-kotlin] |
8989
| [136][136-question] | [Single Number][136-tips] | [Easy][E] | | | [][136-kotlin] |
90+
| [141][141-question] | [Linked List Cycle][141-tips] | [Easy][E] | [][141-java] | | |
9091
| [226][226-question] | [Invert Binary Tree][226-tips] | [Easy][E] | [][226-java] | [][226-js] | [][226-kotlin] |
9192
| [504][504-question] | [Base 7][504-tips] | [Easy][E] | | | [][504-kotlin] |
9293
| [543][543-question] | [Diameter of Binary Tree][543-tips] | [Easy][E] | [][543-java] | | [][543-kotlin] |
@@ -310,6 +311,7 @@
310311
[122-tips]: ./tips/122/README.md
311312
[125-tips]: ./tips/125/README.md
312313
[136-tips]: ./tips/136/README.md
314+
[141-tips]: ./tips/141/README.md
313315
[226-tips]: ./tips/226/README.md
314316
[504-tips]: ./tips/504/README.md
315317
[543-tips]: ./tips/543/README.md
@@ -461,6 +463,7 @@
461463
[119-java]: ./src/_119/Solution.java
462464
[121-java]: ./src/_121/Solution.java
463465
[122-java]: ./src/_122/Solution.java
466+
[141-java]: ./src/_141/Solution.java
464467
[226-java]: ./src/_226/Solution.java
465468
[543-java]: ./src/_543/Solution.java
466469
[554-java]: ./src/_554/Solution.java

src/_141/Solution.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package _141;
2+
3+
import structure.ListNode;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) {
11+
* val = x;
12+
* next = null;
13+
* }
14+
* }
15+
*
16+
* @author relish
17+
* @since 2018/11/15
18+
*/
19+
public class Solution {
20+
21+
public boolean hasCycle(ListNode head) {
22+
ListNode fast = head, slow = head;
23+
if (fast == null) return false;
24+
while (slow != null && fast.next != null) {
25+
slow = slow.next;
26+
fast = fast.next.next;
27+
if (slow == fast) {
28+
return true;
29+
}
30+
if (fast == null) break;
31+
}
32+
return false;
33+
}
34+
35+
public static void main(String[] args) {
36+
System.out.println(new Solution().hasCycle(ListNode.createTestData("[1,2]")));
37+
}
38+
}

tips/141/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[Linked List Cycle][title]
2+
3+
## Description
4+
Given a linked list, determine if it has a cycle in it.
5+
6+
Follow up:
7+
Can you solve it without using extra space?
8+
9+
10+
**Tags:**
11+
Linked List、Two Pointers
12+
13+
14+
## 思路
15+
16+
java(0ms/100.00%):
17+
简单举个例子, 两个人在田径场上绕圈跑步, A的速度为1, B的速度为2, 总有某个时刻, B会追上A。(B和A在同一个位置)
18+
```java
19+
public class Solution {
20+
21+
public boolean hasCycle(ListNode head) {
22+
ListNode fast = head, slow = head;
23+
if (fast == null) return false;
24+
while (slow != null && fast.next != null) {
25+
slow = slow.next;
26+
fast = fast.next.next;
27+
if (slow == fast) {
28+
return true;
29+
}
30+
if (fast == null) break;
31+
}
32+
return false;
33+
}
34+
}
35+
```
36+
37+
## 结语
38+
39+
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
40+
41+
[title]: https://leetcode.com/problems/xxxx
42+
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)