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

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

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

     1 public class Solution {
     2     public ListNode detectCycle(ListNode head) {
     3         // IMPORTANT: Please reset any member data you declared, as
     4         // the same Solution instance will be reused for each test case.
     5         ListNode fast = head, slow = head;
     6         while(fast != null && fast.next != null){
     7             fast = fast.next.next;
     8             slow = slow.next;
     9             if(fast == slow){
    10                 slow = head;
    11                 while(fast != slow){
    12                     fast = fast.next;
    13                     slow = slow.next;
    14                 }
    15                 return slow;
    16             }
    17         }
    18         return null;
    19     }
    20 }
  • 相关阅读:
    asp.net读取/导入project(mpp)文件
    hdu2103
    hdu2100(大数加)
    hdu1406
    hdu1249
    hdu1038
    hdu2565
    hdu1203
    zoj3501
    hdu2102
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3442667.html
Copyright © 2011-2022 走看看