Given the head of a linked list, determine whether a cycle exists and return the node where the cycle begins if one is present.
You are given the head of a singly linked list. The list may contain a cycle: following the next pointers may eventually lead back to an earlier node.
Return the node where the cycle begins. If the list does not contain a cycle, return null.
The answer must identify the actual node object, not just its value.
next pointer.null if no cycle exists.head: head of a singly linked listval and nextnull if the list is acyclicExample 1
Input
head = [3,2,0,-4], pos = 1
Output
node with value 2
Explanation
The tail connects back to the second node, so the cycle starts at the node whose value is 2.
Example 2
Input
head = [1,2], pos = 0
Output
node with value 1
Explanation
The last node points back to the head, so the cycle begins at the first node.
Example 3
Input
head = [1], pos = -1
Output
null
Explanation
The list does not contain a cycle.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.