Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
尋找環的入口節點。
代碼:
參考代碼
解題思路:根據環的判斷,找到快慢指針的交點,然后slow和p一步一步的走,直到相交。
思路二:先求出環的節點的個數k,然后讓p1走k步, 接著p1, p2一起同步走,直到相遇。
代碼:
思路2
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
尋找環的入口節點。
解題思路:根據環的判斷,找到快慢指針的交點,然后slow和p一步一步的走,直到相交。
思路二:先求出環的節點的個數k,然后讓p1走k步, 接著p1, p2一起同步走,直到相遇。