找到带环链表环入口的节点

# -*- 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 
        
        node_set = set()
        while pHead:
            if pHead in node_set:
                return pHead
            else:
                node_set.add(pHead)
                pHead = pHead.next
        return None
        

猜你喜欢

转载自blog.csdn.net/qq_36533552/article/details/125352330