LeetCode【141】Linked List Cycle

Given a linked list, determine if it has a cycle in it.

快慢步,直接AC代码:

值得注意一下的地方就是if(!p2->next || !p2->next->next),如果p2==NULL,那么p2->next用法是错误的,而||运算符的性质是当前一个条件为真时,第二个条件不再判断。

bool hasCycle(ListNode *head) {
        if(!head || !head->next)
            return false;
        ListNode *p1= head;
        ListNode *p2= head;
        while(p2)
        {

            if(!p2->next || !p2->next->next)
                return false;
            p2=p2->next->next;
            p1=p1->next;
            if(p1==p2)
                return true;
        }
        return false;
    }
时间: 2024-07-30 13:40:13

LeetCode【141】Linked List Cycle的相关文章

【LeetCode】【Python】Linked List Cycle

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 思路:笨办法是每个节点再开辟一个属性存放是否访问过,这样遍历一遍即可知道是否有环.但为了不增加额外的空间,可以设置两个指针,一个一次走一步,另一个一次走两步,如果有环则两个指针一定会再次相遇,反之则不会. # Definition for singly-linked list.

【LeetCode】【C++】Linked list cycle 2

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 此题不难,但想不用extra space做出来还是要动点脑筋的,因为我之前看过类似的算法题,所以就很快想到了. 方法是利用两个指针从头开始,指针p1一次走一步,指针p2一次走两步,如果有环则两指针必

【leetcode82】Linked List Cycle

题目描述: 判断有序list是不是环 要求: 时间复杂度o(n) 原文描述: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 思路: 设置两个指针,一个快指针,每次走两步,一个慢指针,每次走一步 如果块指针==慢指针,那么包含环 注意判断空值和长度为一的情况 - 代码: /** * Definition for singly

【链表】Linked List Cycle

题目: Given a linked list, determine if it has a cycle in it. 思路: 对于判断链表是否有环,方法很简单,用两个指针,一开始都指向头结点,一个是快指针,一次走两步,一个是慢指针,一次只走一步,当两个指针重合时表示存在环了. fast先进入环,在slow进入之后,如果把slow看作在前面,fast在后面每次循环都向slow靠近1,所以一定会相遇,而不会出现fast直接跳过slow的情况. /** * Definition for singly

【LeetCode】Linked List Cycle

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 做完Linked List Cycle II在做这题简直就是阉割版.. fast每次前进两步,slow每次前进一步,如果相遇则为true,否则false class Solution { public: bool hasCycle(ListNo

【Leetcode】Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 思路:由[Leetcode]Linked List Cycle可知,利用一快一慢两个指针能够判断出链表是否存在环路.假设两个指针相遇之前slow走了s步,则fast走了2s步,并且fast已经在长度

【leetcode刷题笔记】Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 判断一个链表是否有环. 题解: 设置两个指针p1和p2: p1每次走一步,p2每次走两步,如果在这个过程中,p2为空,则没有环:否则两个指针必然相遇,则有环: 接下来找环的起点,将p1挪动到链表起始,

【leetcode】Linked List Cycle (python)

题目分析见这里 class Solution: # @param head, a ListNode # @return a list node def detectCycle(self, head): if None == head or None == head.next: return None pfast = head pslow = head #找第一次相遇的点,若存在环路,则肯定会相遇 while pfast and pfast.next: pfast = pfast.next.nex

LeetCode之“链表”:Linked List Cycle && Linked List Cycle II

1. Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 刚看到这道题,很容易写出下边的程序: 1 bool hasCycle(ListNode *head) { 2 ListNode *a = head, *b = head; 3 while(a) 4 { 5 b =