public boolean hasCycle(ListNode head) {

		if (head == null)
			return false;

		ListNode fast = head;
		ListNode slow = head;

		if (fast.next != fast && fast.next != null)
			fast = fast.next;

		while (fast.next != null && fast.next.next != null) {
			if (fast == slow)
				return true;
			fast = fast.next.next;
			//if (slow.next != null)  //not necessary cause fast already checked!.
            slow = slow.next;
		}

		return false;

	}