class HasCycleInLinkedList{
public boolean hasCycle(ListNode head){
if(head == null  head.next == null) return false;
if(head.next == head) return true;
ListNode nextNode = head.next;
head.next = head;
boolean isCycle = hasCycle(nextNode);
return isCycle;
}
}
Shorter Solution in Java

If modifying the linked list is OK then it can done even shorter:
public boolean hasCycle(ListNode head) { while( head != null ) { if( head.val == 0xcafebabe ) return true; head.val = 0xcafebabe; // Mark this node as visited head = head.next; } return false; }
even more shorter:
public boolean hasCycle(ListNode head) { if ( head == null ) return false; if( head.val == 0xcafebabe ) return true; head.val = 0xcafebabe; // Mark this node as visited return hasCycle(head.next); }