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

    https://leetcode.com/problems/linked-list-cycle-ii/#/description

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
    
    Note: Do not modify the linked list.
    
    Follow up:
    Can you solve it without using extra space?
    public ListNode detectCycle(ListNode head) {
          
            if (head == null) {
                return null;
            }
            ListNode fast = head;
            ListNode slow = head;
            do {
                if (fast == null || fast.next == null) {
                    return null;
                }
                fast = fast.next.next;
                slow = slow.next;
            } while (fast != slow);
            fast = head;
            while (fast != slow) {
                fast = fast.next;
                slow = slow.next;
            }
            return fast;
        }
    

      

  • 相关阅读:
    Mysql优化之Explain查询计划查看
    map转listmap
    代码大全
    cas
    日志
    xml模板
    springboot
    日志
    spring应用
    拆分表中sheet
  • 原文地址:https://www.cnblogs.com/apanda009/p/7123253.html
Copyright © 2011-2022 走看看