← 返回 amazon 的题目列表Find Middle and Cycle Start in Linked List
类型:online_judge
amazon
Given a singly linked list, write a function to find the middle element of the list. If the list contains a cycle, find the start node of the cycle. The function should return two values: the middle node and the start node of the cycle if it exists.
Input Description:
Head of the linked list.
Output Description:
Middle node.
If the list contains a cycle, return the start node of the cycle.
Note:
If the list length is even, the middle node is the second of the two middle nodes.
Examples:
Example 1: Input: [1, 2, 3, 4, 5] Output: middle node is 3
Example 2: Input: [1, 2, 3, 4, 5, 6] Output: middle node is 4
Example 3 (with cycle): Input: [3, 2, 0, -4], cycle at index 1 Output: cycle detected, start node is 2
Example
Input
1 2 3 4 5 -1 -1