链表中环的入口节点
链表中环的入口节点
http://www.nowcoder.com/questionTerminal/6e630519bf86480296d0f1c868d425ad
/** * 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) { ListNode node = head; ListNode next = head; while(node != null && next != null){ node = node.next; next = next.next; if(next != null){ next = next.next; } if(node == next){ break; } } if(next == null || node == null){ return null; } while(next != head){ next = next.next; head = head.next; } return next; } }