快慢指针
代码public class Solution { public ListNode detectCycle(ListNode head) { if (head == null){ return head; } ListNode fast = head; ListNode slow = head; while (fast.next != null && slow.next != null && fast.next.next!=null){ fast = fast.next.next; slow = slow.next; if (fast == slow){ fast = head; while (slow != fast){ slow = slow.next; fast = fast.next; } return slow; } } return null; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)