1 /** 2 * Definition for singly-linked list. 3 * class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 public boolean hasCycle(ListNode head) { 14 // IMPORTANT: Please reset any member data you declared, as 15 // the same Solution instance will be reused for each test case. 16 if(head == null){ 17 return false; 18 } 19 20 ListNode fast = head, slow = head; 21 while(fast.next != null && fast.next.next != null){ 22 fast = fast.next.next; 23 slow = slow.next; 24 if(fast == slow){ 25 return true; 26 } 27 } 28 return false; 29 } 30 }