编程笔试(解析及代码实现):链表中环的入口节点。给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null
目录
题目描述
链表中环的入口节点。给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
代码实现
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
# write code here
#遍历链表,环的存在,遍历遇见的第一个重复的即为入口节点
tempList = []
p = pHead
while p:
if p in tempList:
return p
else:
tempList.append(p)
p = p.next