本文共 815 字,大约阅读时间需要 2 分钟。
Linked List Cycle II
null
. /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode detectCycle(ListNode head) { try { ListNode slow = head; ListNode fast = head; do { slow = slow.next; fast = fast.next.next; } while (fast != slow); fast = head; while (fast != slow) { slow = slow.next; fast = fast.next; } return slow; } catch (NullPointerException e) { return null; } }}
转载地址:http://ciuni.baihongyu.com/