Given head, the head of a linked list, determine if the linked list has a cycle in it.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the&nbsp;next&nbsp;pointer. Internally, pos&nbsp;is used to denote the index of the node that&nbsp;tail's&nbsp;next&nbsp;pointer is connected to.&nbsp;Note that&nbsp;pos&nbsp;is not passed as a parameter.

Return&nbsp;true if there is a cycle in the linked list. Otherwise, return false.

&nbsp;
Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).


Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.


Example 3:

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.


&nbsp;
Constraints:


	The number of the nodes in the list is in the range [0, 104].
	-105 &lt;= Node.val &lt;= 105
	pos is -1 or a valid index in the linked-list.


&nbsp;
Follow up: Can you solve it using O(1) (i.e. constant) memory?

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        for i in range(0, 10001):
            if head == None: return False
            head = head.next
        
        return True
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        boolean result = false;
        while(fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
                if(fast == slow){
                    result =  true;
                    break;
                }
        }
        return result;
    }
}
class Solution {
public:
    bool hasCycle(ListNode *head) {
	
		// if head is NULL then return false;
        if(head == NULL)
            return false;
        
		// making two pointers fast and slow and assignning them to head
        ListNode *fast = head;
        ListNode *slow = head;
        
		// till fast and fast-> next not reaches NULL
		// we will increment fast by 2 step and slow by 1 step
        while(fast != NULL && fast ->next != NULL)
        {
            fast = fast->next->next;
            slow = slow->next;
            
			
			// At the point if fast and slow are at same address
			// this means linked list has a cycle in it.
            if(fast == slow)
                return true;
        }
        
		// if traversal reaches to NULL this means no cycle.
        return false;
    }
};
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
function hasCycle(head){
    
  let fast = head;
  while (fast && fast.next) {
    head = head.next;
    fast = fast.next.next;
    if (head === fast) return true;
  }
   //head and first pointers value in 
  //each iteration with head=[3,2,0,-4], pos = 1
  //1st iteration: 3 3
  //2nd iteration: 2 0
  //3rd iteration: 0 2
  //final iteration: -4 -4
  return false;
};
Linked List Cycle