Skip to content

Commit 8c9adaf

Browse files
committed
fd
1 parent c754381 commit 8c9adaf

File tree

3 files changed

+26
-43
lines changed

3 files changed

+26
-43
lines changed

solution/src/main/java/com/inuker/solution/LinkedListCycle.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@
1010
public class LinkedListCycle {
1111

1212
public boolean hasCycle(ListNode head) {
13-
if (head == null) {
14-
return false;
15-
}
16-
17-
ListNode fast = head.next, slow = head;
18-
19-
for ( ; fast != null && fast.next != null; fast = fast.next.next, slow = slow.next) {
13+
for (ListNode fast = head, slow = head; fast != null && fast.next != null; ) {
14+
slow = slow.next;
15+
fast = fast.next.next;
2016
if (fast == slow) {
2117
return true;
2218
}
2319
}
24-
2520
return false;
2621
}
2722
}

solution/src/main/java/com/inuker/solution/LinkedListCycleII.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,26 @@
77
*/
88

99
public class LinkedListCycleII {
10-
10+
/**
11+
* 这里有个问题,如果链表是a,b,然后又回到a
12+
* 则slow和fast必须都从head起步才行
13+
* 如果slow从head起步,fast从head.next起步,则这里会死循环
14+
*/
1115
public ListNode detectCycle(ListNode head) {
12-
if (head == null || head.next == null) return null;
13-
14-
ListNode firstp = head;
15-
ListNode secondp = head;
16-
boolean isCycle = false;
17-
18-
while (firstp != null && secondp != null) {
19-
firstp = firstp.next;
20-
if (secondp.next == null) return null;
21-
secondp = secondp.next.next;
22-
if (firstp == secondp) {
23-
isCycle = true;
16+
ListNode slow = head, fast = head;
17+
boolean hasCycle = false;
18+
for (; fast != null && fast.next != null; ) {
19+
slow = slow.next;
20+
fast = fast.next.next;
21+
if (slow == fast) {
22+
hasCycle = true;
2423
break;
2524
}
2625
}
27-
28-
if (!isCycle) return null;
29-
firstp = head;
30-
while (firstp != secondp) {
31-
firstp = firstp.next;
32-
secondp = secondp.next;
26+
if (!hasCycle) {
27+
return null;
3328
}
34-
35-
return firstp;
29+
for (ListNode node = head; node != slow; node = node.next, slow = slow.next);
30+
return slow;
3631
}
3732
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,14 @@ public class main {
2424
public static void main(String[] args) {
2525
}
2626

27-
28-
public ListNode insertionSortList(ListNode head) {
29-
ListNode dummy = new ListNode(0);
30-
for (ListNode next; head != null; head = next) {
31-
next = head.next;
32-
for (ListNode cur = dummy; cur != null; cur = cur.next) {
33-
if (cur.next != null && head.val > cur.next.val) {
34-
continue;
35-
}
36-
head.next = cur.next;
37-
cur.next = head;
38-
break;
27+
public boolean hasCycle(ListNode head) {
28+
for (ListNode fast = head, slow = head; fast != null && fast.next != null; ) {
29+
slow = slow.next;
30+
fast = fast.next.next;
31+
if (fast == slow) {
32+
return true;
3933
}
4034
}
41-
return dummy.next;
35+
return false;
4236
}
43-
4437
}

0 commit comments

Comments
 (0)