The title description of a circular linked list
If there is a node in the list that can be reached again by following the next pointer continuously, then there is a ring in the list. To represent the rings in a given list, the metric system internally uses the integer POS to represent where the tail of the list is connected to in the list (the index starts at 0). If pos is -1, there are no rings in the list; Returns true if there are rings in the list. Otherwise, return false
Example 1
Input: head = [3,2,0,-4], pos = 1 output: true explanation: there is a ring in the linked list whose tail is connected to a second node.Copy the code
Tip:
- The number of nodes in a linked list ranges from
[0, 10 to the fourth]
- 105 <= Node.val <= 105
pos
为- 1
Or one of the linked listsEffective index
Here is the code rendering
Var hasCycle = function(head){var hasCycle = function(head){var hasCycle = function(head){ Also returns false / / so we need to make a judgment if (head = = = null | | head. The next = = = null) return false. // We can traverse the list from beginning to end, and see if the current node has a tag. If not, we can tag it with a tag, and then we can prove that we have reached the node. // If we have not encountered this node, we will go to the end of the list and point to null. Let cur = head while(cur){return true if(cur.tag) return true else{// If (cur.tag) = "a" // Next}} return false}Copy the code