Leetcode.com/problems/li…
Discuss:www.cnblogs.com/grandyang/p…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
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 next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.
Notice that you should not modify the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Copy the code
Example 2:
Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Copy the code
Example 3:
Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.
Copy the code
Constraints:
The number of the nodes in the list is in the range [0, 104].
-105 <= Node.val <= 105
pos is -1 or a valid index in the linked-list.
Follow up: Can you solve it using O(1) (i.e. constant) memory?
Solution a:
Use HashSet records.
/** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ class Solution { fun detectCycle(head: ListNode?) : ListNode? { if (head? .next == null) { return null } val cacheSet = hashSetOf<ListNode>() var tempHead = head while (tempHead ! = null) { cacheSet.add(tempHead) if (cacheSet.contains(tempHead.next)) { return tempHead.next } tempHead = tempHead.next } return null } }Copy the code
Method 2:
Fast and slow pointer solution. The starting point of finding the rings in the single Linked List is the previous one to determine whether there is an extension of the rings in the single Linked List. Please refer to the previous Linked List Cycle. Here is how to set up a pointer, but this time to record the location of the two Pointers to meet, when met two Pointers, let one of the pointer from the beginning of the head, meet again at this point location is central, list the starting position, why is this, here directly with enthusiastic netizen “birds want to fly” explanation, because every time quick pointer walk 2, slow pointer walk 1 at a time, The fast pointer goes twice as far as the slow pointer. And the fast hand has gone one more time than the slow hand. So the distance from the head to the start of the ring + the start of the rings to the point where they meet is the same as the distance of the circle. So, the distance from head to the beginning of the ring is the same as the distance from the encounter point to the beginning of the ring.
/** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ class Solution { fun detectCycle(head: ListNode?) : ListNode? { if (head? .next == null) { return null } var slowNode: ListNode? = head var quickNode: ListNode? = head while (quickNode? .next ! = null) { slowNode = slowNode? .next quickNode = quickNode.next? .next if (slowNode == quickNode) { break } } if (quickNode? .next == null) { return null } slowNode = head while (slowNode ! = quickNode) { slowNode = slowNode? .next quickNode = quickNode? .next } return quickNode } }Copy the code