zoukankan      html  css  js  c++  java
  • [LeetCode#142]Linked List Cycle II

    The question:

    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?

    The analysis:

    This question is very interesting, but a little tricky. 
    key idea: Use the proper distance relationship analysis to get the clear idea and answer. But you need very careful about the corner cases.
    Classic method: use two pointers.
    1. walker pointer: each time, the pointer move one step;
    2. runner pointer: each time, the pointer move two steps.
    As if there is a circle in the list, the walker pointer and runner pointer must would meet each other.
    while (runner.next != null && runner.next.next != null) {
        walker = walker.next;
        runner = runner.next.next;
        if (walker == runner) {
            is_cycle = true;
            break;
        }
    }
    How to get the node where the cycle starts?
         a     start      b
    ------------o----------------o(collision point)
                |                |
                |                |
                |----------------|                
                         c
    since the runner's pace is the twice speed of a, we must meet the condition:
    a+n(b+c)+b = 2(a+b) 
    we could infer:
    n(b+c) = a+b  ===> n(b+c)-b = a
    This means that if one pointer start from the head, and one pointer start from the collision point, and they are in the same pace, they would meet each other at the start point.
    walker = head;
    while (walker != runner) {
        walker = walker.next;
        runner = runner.next; //the runner's pace is adjusted!
    }
    return walker;
    
    Note: there is a hidden buggy condition. 
    If we write the loop in following way: 
    walker = head;
    while (runner.next != null) {
        walker = walker.next;
        runner = runner.next; //the runner's pace is adjusted!
        if (walker == runner)
            break;
    }
    return walker;
    
    The fragment seems to be right at first sight. However, it could not tackle the corner case:
    node1 -> node2 -> node1
    At this case, the collision point would be node1. it means we start walker and runner at the same position, it would lead to walker == runner at node2, which would result in the wrong answer. 
    A little change in code would solve the corner case:
    walker = head;
    while (walker != runner) {
        walker = walker.next;
        runner = runner.next; //the runner's pace is adjusted!
    }
    return walker;

    My solution:

    public class Solution {
        public ListNode detectCycle(ListNode head) {
            if (head == null)
                return null;
            ListNode walker = head;
            ListNode runner = head;
            boolean is_cycle = false;
            while (runner.next != null && runner.next.next != null) {
                walker = walker.next;
                runner = runner.next.next;
                if (walker == runner) {
                    is_cycle = true;
                    break;
                }
            }
            if (is_cycle != true) //if there is no cycle exist.  
                return null;
            walker = head;
            while (walker != runner) {
                walker = walker.next;
                runner = runner.next; //the runner's pace is adjusted!
            }
            return walker;
        }
    }
  • 相关阅读:
    PHP设计模式
    PHP设计模式
    PHP 23种设计模式
    MySQL 中的共享锁和排他锁的用法
    PHP_MySQL高并发加锁事务处理
    Connection: close和Connection: keep-alive有什么区别
    罗辑思维首席架构师:Go微服务改造实践
    真诚与尊重是技术团队的管理要点
    10种常见的软件架构模式
    百亿级微信红包的高并发资金交易系统设计方案
  • 原文地址:https://www.cnblogs.com/airwindow/p/4235130.html
Copyright © 2011-2022 走看看