题解 | #链表中环的入口结点#

链表中环的入口结点

http://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4

采用集合的方式解决

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        set1 = set()  ### 一个集合
        while pHead:
            if pHead.val not in set1:   #### 如果不在,则添加
                set1.add(pHead.val)
                pHead = pHead.next
            else:                       ### 如果存在说明是第二次出现,那必须是环的入口,因为只有环的入口才是第一个存在重复的结点
                    return pHead
        

全部评论

相关推荐

一天代码十万三:这都不能算简历吧
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务