File tree Expand file tree Collapse file tree 3 files changed +26
-43
lines changed
solution/src/main/java/com/inuker/solution
test/src/main/java/com/inuker/test Expand file tree Collapse file tree 3 files changed +26
-43
lines changed Original file line number Diff line number Diff line change 10
10
public class LinkedListCycle {
11
11
12
12
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 ;
20
16
if (fast == slow ) {
21
17
return true ;
22
18
}
23
19
}
24
-
25
20
return false ;
26
21
}
27
22
}
Original file line number Diff line number Diff line change 7
7
*/
8
8
9
9
public class LinkedListCycleII {
10
-
10
+ /**
11
+ * 这里有个问题,如果链表是a,b,然后又回到a
12
+ * 则slow和fast必须都从head起步才行
13
+ * 如果slow从head起步,fast从head.next起步,则这里会死循环
14
+ */
11
15
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 ;
24
23
break ;
25
24
}
26
25
}
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 ;
33
28
}
34
-
35
- return firstp ;
29
+ for ( ListNode node = head ; node != slow ; node = node . next , slow = slow . next );
30
+ return slow ;
36
31
}
37
32
}
Original file line number Diff line number Diff line change @@ -24,21 +24,14 @@ public class main {
24
24
public static void main (String [] args ) {
25
25
}
26
26
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 ;
39
33
}
40
34
}
41
- return dummy . next ;
35
+ return false ;
42
36
}
43
-
44
37
}
You can’t perform that action at this time.
0 commit comments