链表中环的入口结点(单链表)

来源:互联网 发布:2017网络规划设计师 编辑:程序博客网 时间:2024/06/01 16:24

题目描述

一个链表中包含环,请找出该链表的环的入口结点。


把所有结点依次存入列表中,每一次存入之前进行判断该结点在链表中是否出现过即可。

# -*- coding:utf-8 -*-# class ListNode:#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution:    def EntryNodeOfLoop(self, pHead):        # write code here        res = []        while pHead:        if pHead in res:        return pHead        res.append(pHead)        pHead = pHead.next        return None


0 0