标签:leetcode
https://oj.leetcode.com/problems/linked-list-cycle-ii/
http://blog.csdn.net/linhuanmars/article/details/21260943
/** * 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) { if (head == null) return null; ListNode one = head; ListNode two = head; ListNode meet = null; while (one != null && two != null) { one = one.next; two = two.next; if (two == null) return null; else two = two.next; if (one == two) { meet = one; break; } } if (meet == null) return null; // no cicle. one = head; while (one != two) { one = one.next; two = two.next; } return one; } }
[LeetCode]142 Linked List Cycle II
标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1600801