-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteCycle.java
More file actions
32 lines (32 loc) · 779 Bytes
/
DeleteCycle.java
File metadata and controls
32 lines (32 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class DeleteCycle
{
public ListNode detectCycle(ListNode head)
{
ListNode fast = head;
ListNode slow = head;
boolean is_cycle = false;
while (fast != null && fast.next != null)
{
fast = fast.next.next;
slow = slow.next;
if (fast == slow)
{
is_cycle = true;
break;
}
}
if (!is_cycle)
return null;
else
{
ListNode indexhead = head;
ListNode indexmeet = slow;
while (indexhead != indexmeet)
{
indexhead = indexhead.next;
indexmeet = indexmeet.next;
}
return indexhead;
}
}
}