题解 | #链表中环的入口结点#
链表中环的入口结点
http://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
标记法
遍历链表,标记访问过的节点,若标记过的节点被再次访问,则为环的入口。
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
while pHead:
# 找到环的入口
if pHead.val > 10000:
pHead.val -= 10000
return pHead
# 用标记访问过的节点
pHead.val += 10000
# 向后移动
pHead = pHead.next
return None