141. Linked List Cycle#
1public boolean hasCycle(ListNode head) {
2 ListNode slowPointer = head;
3 ListNode fastPointer = head;
4
5 while (fastPointer != null && fastPointer.next != null) {
6 slowPointer = slowPointer.next;
7 fastPointer = fastPointer.next.next;
8
9 if (slowPointer == fastPointer) {
10 return true;
11 }
12 }
13
14 return false;
15}