牛客网上的剑指 offer的在线编程:
题目描述
一个链表中包含环,请找出该链表的环的入口结点。
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
# write code here
# 方法:用一个列表记录每一个节点,若出现重复节点,则此节点为入口节点
if pHead is None:
return
node = []
node.append(pHead)
p1 = pHead.next
while p1:
if p1 in node:
return p1
node.append(p1)
p1 = p1.next