zoukankan      html  css  js  c++  java
  • 102. Linked List Cycle【medium】

    Example

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

    Challenge Follow up:
    Can you solve it without using extra space?

    解法一:

     1 class Solution {
     2 public:
     3     /**
     4      * @param head: The first node of linked list.
     5      * @return: True if it has a cycle, or false
     6      */
     7     bool hasCycle(ListNode *head) {
     8         ListNode * fast, * slow;
     9         if (head == NULL) {
    10             return false;
    11         }
    12         slow = head;
    13         fast = head->next;
    14         
    15         while (fast != NULL && fast->next != NULL) {
    16             if (slow == fast) {
    17                 return true;
    18             }
    19             slow = slow->next;
    20             fast = fast->next->next;
    21         }
    22         
    23         return false;
    24     }
    25 };
  • 相关阅读:
    Alpha冲刺
    Alpha冲刺
    抽奖系统(记一次未完成的教训)
    Alpha冲刺
    软件工程
    软工实践
    软工实践
    软工实践
    软工实践
    软工实践
  • 原文地址:https://www.cnblogs.com/abc-begin/p/8157977.html
Copyright © 2011-2022 走看看