Practice/Salesforce/Leetcode 142. Linked List Cycle II
CodingOptional
Given the head of a singly linked list, determine if the list contains a cycle. If a cycle exists, return the node where the cycle begins. If there is no cycle, return null.
A cycle occurs when a node's next pointer points back to a previous node in the list, creating a loop. The cycle does not necessarily start at the beginning of the list.
nullpos indicates the index of the node that the tail connects to (0-indexed), or -1 if there is no cycleExample 1:
Input: head = [3,2,0,-4], pos = 1 Output: Node with value 2 Explanation: The tail of the list connects back to the node at index 1, which has value 2. This is where the cycle begins.
Example 2:
Input: head = [1,2], pos = 0 Output: Node with value 1 Explanation: The tail connects back to the first node, creating a cycle at the head of the list.
Example 3:
Input: head = [1], pos = -1 Output: null Explanation: There is no cycle in this single-node list.