Problem: Linked List Cycle
Description
Given a singly linked list, determine whether the linked list has a cycle (i.e., a node points to one of its previous nodes).
Problem Statement
To solve this problem, you need to write an algorithm that takes the head node of a singly linked list as input and returns a boolean indicating whether the linked list has a cycle.
Rules and Constraints
- The linked list has at least one node.
- The linked list is represented as a singly linked structure, where each node contains an integer value and a reference to the next node in the list.
- You are not allowed to use any additional data structures other than the linked list itself.
- You can assume that the linked list nodes will be non-null.
- You need to solve this problem in O(n) time complexity, where n is the number of nodes in the linked list.
- You need to solve this problem with O(1) space complexity, which means you should not use any additional variables outside of the space required to store the linked list nodes.
Expected Output
Your algorithm should return a boolean value indicating whether the linked list has a cycle. If the linked list has a cycle, your algorithm should return true. Otherwise, it should return false.
Example
Input: {"head":[3,2,0,-4],"pos":1}
Output: true