【LeetCode从零单刷】Linked List Cycle

题目:

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

Can you solve it without using extra space?

解答:

乍一看很简单。我的做法是保存头指针,然后另一个指针继续遍历下去,直到遇到头指针为止。但是我没有考虑到部分回环情况:

说实话我并没有想到什么好办法。直到看了Discussion,发现可以有追击问题的思路来做:一个慢指针一次跑一步,一个快指针一次跑两步。如果有环一定能追上。

于是我写下:

class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head == NULL || head->next == NULL || head->next->next == NULL)
            return false;

        ListNode* slow = head->next;
        ListNode* fast = head->next->next;
        while(slow != NULL)
        {
            if(slow == fast)
            {
                return true;
            }

            if(fast->next->next == NULL)
            {
                return false;
            }
            else
            {
                slow = slow->next;
                fast = fast->next->next;
            }
        }
        return false;
    }
};

显示Runtime Error……后来发现,当判断 if(fast->next->next == NULL) 的时候,我忘记了考虑了:如果 fast->next == NULL,那 fast->next->next 就变成了野指针,然后else里面将野指针赋值给 fast 就没尽头了。

将判断 if(fast->next->next == NULL)  改为了 if (fast->next->next == NULL || fast->next == NULL),还是错……

这时基本功暴露了……条件“或”操作是短路操作。而 fast->next == NULL 是需要提前判断的条件,所以应该提前判断。最终AC版本:

class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head == NULL || head->next == NULL || head->next->next == NULL)
            return false;

        ListNode* slow = head->next;
        ListNode* fast = head->next->next;
        while(slow != NULL)
        {
            if(slow == fast)
            {
                return true;
            }

            if(fast->next == NULL || fast->next->next == NULL)
            {
                return false;
            }
            else
            {
                slow = slow->next;
                fast = fast->next->next;
            }
        }
        return false;
    }
};
时间: 2024-10-08 14:52:26

【LeetCode从零单刷】Linked List Cycle的相关文章

【LeetCode从零单刷】Binary Tree Inorder Traversal

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2].  Note: Recursive solution is trivial, could you do it iteratively? 解答: 其实思路有点类似以前的一篇文章:[LeetCode从零单刷]Binary

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 day5 -- Reorder List && Linked List Cycle II

1.  Reorder List Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. 分析:翻转链表是很常见的题目,这种翻转是第一次见.一开始

【LeetCode从零单刷】Maximum Depth of Binary Tree

没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: Given a binary tree, find its maximum depth.  The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 解答: 树的深度优先遍历,求得树高度即可.然后需要用到递归的思想,假设子树的高

【LeetCode从零单刷】Same Tree

没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: Given two binary trees, write a function to check if they are equal or not.  Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解答: 验证两棵树是否相等.使

LeetCode刷题:Linked List Cycle 及其进阶Linked List Cycle II

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 1. 首先解决的问题是 判断单链表 有没有环? 解题思路: 两个指针slow 和 fast,开始都指向 head, slow指针每次走一步,fast 指针每次走两步,看看最终 slow 和 fast 会不会重合,如果 链表有环,那么一定会重合. /** * Definition

(LeetCode 141/142)Linked List Cycle

1.Given a linked list, determine if it has a cycle in it. 2.Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 3.Given a linked list, return the length of the cycle, if there is no cycle, return 0; 题目要求: 1

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

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more