zoukankan      html  css  js  c++  java
  • Linked List Cycle

     1 /**
     2  * Definition for singly-linked list.
     3  * class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) {
     7  *         val = x;
     8  *         next = null;
     9  *     }
    10  * }
    11  */
    12 public class Solution {
    13     public boolean hasCycle(ListNode head) {
    14         // IMPORTANT: Please reset any member data you declared, as
    15         // the same Solution instance will be reused for each test case.
    16         if(head == null){
    17             return false;
    18         }
    19         
    20         ListNode fast = head, slow = head;
    21         while(fast.next != null && fast.next.next != null){
    22             fast = fast.next.next;
    23             slow = slow.next;
    24             if(fast == slow){
    25                 return true;
    26             }
    27         }
    28         return false;
    29     }
    30 }
  • 相关阅读:
    树状数组&线段树
    8月7日小练
    8月6日小练
    LID&LDS 的另外一种算法
    LCS,LIS,LCIS
    8-11-Exercise
    8-10-Exercise
    线段树
    8-7-Exercise
    8-6-Exercise
  • 原文地址:https://www.cnblogs.com/jasonC/p/3439755.html
Copyright © 2011-2022 走看看