题目
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null) return false;
ListNode n1 = head, n2 = head;
while(n2.next != null && n2.next.next != null) {
n1 = n1.next;
n2 = n2.next.next;
if(n1 == n2) return true;
}
return false;
}
}