Lintcode102.Linked List Cycle

Posted Vincent丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lintcode102.Linked List Cycle相关的知识,希望对你有一定的参考价值。

题目:

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

Example

Given -21->10->4->5, tail connects to node index 1, return true

题解:

Solution 1 ()

class Solution {
public:
    bool hasCycle(ListNode *head) {
        if (!head) {
            return false;
        }
        ListNode* fast = head;
        ListNode* slow = head;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast) {
                return true;
            }
        }
        
        return false;
    }
};

 

以上是关于Lintcode102.Linked List Cycle的主要内容,如果未能解决你的问题,请参考以下文章

lintcode-medium-Sort List

lintcode-easy-Reverse Linked List

lintcode-medium-Reorder List

LintCode - Merge Two Sorted List

lintcode-medium-Copy List with Random Pointer

lintcode-medium-Reverse Linked List II