[LeetCode141]Linked List Cycle

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

  判断一个链表是否有环

代码:

  

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *fast(head), *slow(head);
        while(fast && fast->next)
        {
            fast = fast->next->next;
            slow = slow->next;
            if(fast == slow)
                return true;
        }
        return false;
    }
};

关于进一步求环的长度和入口结点可以看另一篇博客http://www.cnblogs.com/zhangbaochong/p/5151986.html

时间: 2024-08-08 04:03:03

[LeetCode141]Linked List Cycle的相关文章

LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

链表相关题 141. 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? (Easy) 分析: 采用快慢指针,一个走两步,一个走一步,快得能追上慢的说明有环,走到nullptr还没有相遇说明没有环. 代码: 1 /** 2 * Definition for singly-linked list.

[LeetCode]Linked List Cycle

题目:Linked List Cycle 判断一个单链表中是否有环,要求常量空间复杂度: 思路: 使用两个指针同时从链表表头开始移动,一个移动一步,一个移动两步,直到两个指针重合或某一指针指向链尾. 两个指针重合则单链表有环存在,否则没有. 第二个指针以第一个指针的两倍的速度移动,而第一个指针每次移动一步,这样只要有环,两个指针必定能重合. bool LeetCode::hasCycle(ListNode *head){ if (!head)return false; ListNode *p =

LeetCode(141): 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? 题意:判断一个链表中是否存在环. 思路:(参考别人的做法)采用"快慢指针"检查链表是否含有环.即让一个指针一次走一步,另一个指针一次走两步,如果链表中含有环,快的指针会再次和慢的指针相遇. 代码: public boolean h

Linked List Cycle

Linked List Cycle Given a linked list, determine if it has a cycle in it. c++ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool ha

Leetcode 链表 Linked List Cycle II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle II Total Accepted: 20444 Total Submissions: 66195My Submissions Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;

【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 && 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 =

Leetcode 142 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 141 Linked List Cycle 性质:distance from head to 环开始点 == distance from 双指针相遇的点 to 环开始点 证明: 指