剑指offer 55. 链表中环的入口结点

题目描述

给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

思路:

z这种题直接引入Python list进行遍历存储,简直不能再省心。

参考答案:

# -*- 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 not pHead:
            return None
        temp = []
        while pHead:
            if pHead in temp:
                return pHead
            else:
                temp.append(pHead)
                pHead = pHead.next
        return None
    
    

猜你喜欢

转载自blog.csdn.net/Dby_freedom/article/details/84504224