LinkedList:给定有环链表,实现一个算法返回环路的开头结

2016-05-18  本文已影响0人  敲一手烂代码
public static Node test6(Node node) {
        Node head = node;
        Node fast = node;
        Node slow = node;
        while (fast.next!=null && fast!=null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast.value == slow.value) {
                break;
            }
        }
        fast = head;
        while (fast.next!=null && fast!=null) {
            fast = fast.next;
            slow = slow.next;
            if (fast.value == slow.value) {
                break;
            }
        }
        
        return fast;
    }
上一篇下一篇

猜你喜欢

热点阅读