zoukankan      html  css  js  c++  java
  • LeetCode-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.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            if(head==NULL)return false;
            ListNode* p1=head,*p2=head;
            
            while(true){
                p1=p1->next;
                if(p1==NULL)return false;
                p2=p2->next;
                if(p2==NULL)return false;
                p2=p2->next;
                if(p2==NULL)return false;
                if(p1==p2)return true;
            }
        }
    };
  • 相关阅读:
    JS高级
    函数作用域面试题
    11.14
    11.13
    Redux知识
    react-router-dom
    react 的三大属性
    vuex
    数组的扩展
    函数作用域和 class
  • 原文地址:https://www.cnblogs.com/superzrx/p/3420773.html
Copyright © 2011-2022 走看看