Leetcode 141. Ring List

tags: leetcode

Leetcode 141. Ring List

Think

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.

Code

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
 }
        

Intelligent Recommendation

leetcode-141-ring linked list

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...

LeetCode Question 141 --- Ring List

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 = ...

(Js) LeetCode 141. Ring List

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...

Leetcode Twopointer 141 Ring List

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...

Leetcode ----- Question 141 ----- Ring List

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...

More Recommendation

[Python] Leetcode 141. Ring List

Leetcode 141. Ring List  ...

Leetcode 141. Ring List | Python

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...

Remember that a DBCP2 connection is not recycled, the program is stuck, and the analysis process

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...

Spring boot 2.5.3 Integration Spring Cloud Alibaba Built NaCOS Service Brake 'ServiceName' Is Illegal, Services IS Blank`

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...

2020 school recruit written Sogou

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top