tags: leetcode
Set two job pointers, one step each time, another one go two steps, start, starting from the list header node, this, if the linked list has a ring, then the slowness will definitely catch up Fast, otherwise, until it is not possible to catch up.
bool hasCycle(ListNode *head) {
ListNode* p1=head;
ListNode* p2=head;
if(!head || !head->next) return NULL; // Pay attention to the detection of whether or not a node or only one node, only one node is not two steps, will be wrong
while(p2 && p2->next){ // When there are two or more nodes
p1=p1->next; // a step
p2=p2->next->next; // Two steps
if(p1==p2){
return true;
}
}
return false; // Traverse the latter list indicates that there is no ring, return false
}
topic: Given a linked list, judge whether there is a ring in the linked list. In order to represent the rings in a given linked list, we use the integer pos to indicate the position where the end of t...
I. Title requirements Given a linked list, determine if there is a ring in the list. Returns true if there is a ring in the list. Otherwise, return false. Example 1 Enter: Head = [3, 2, 0, -4], POS = ...
topic: Given a linked list, determine if there is a ring in the list. If there is a node in the linked list, you can reach again through the continuous tracking of the next Next pointer, there is a ri...
Article catalog O problem achieve Think Summary and reflection Code O problem achieve Think It is judged that the ring is taken with a slow pointer. The quick pointer takes twice once, slow pointer to...
Annular table Difficulty: Simple Given a linked list, determine if there is a ring in the list. In order to express the ring in a given list, we use an integerposTo indicate that the chain tail is con...
141. Ring List topic Given a linked list, determine if there is a ring in the list. In order to indicate the ring in the given list, we use an integer POS to indicate the location of the chain tail to...
I just changed to a new company recently. I want to optimize the previous code and improve the efficiency of task execution. I reviewed the previous code. There are many places to optimize. The compan...
Version description Spring boot and Spring Cloud Alibaba have corresponding relationships. If you do not set the Maven coordinates according to the corresponding version number, you will cause a varie...
Puppy Map Design Title Description Now we need to design aMap<key,val>,The following conditions Map capacity is aThe fixed value N, Save up to N records When the insert operation, the first quer...