首页 > 其他 > 详细

剑指Offer——链表中环的入口节点

时间:2019-07-14 12:11:18      阅读:70      评论:0      收藏:0      [点我收藏+]

1、题目描述

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

2、代码实现

import java.util.HashSet;
import java.util.Set;
public class Solution {

    public ListNode EntryNodeOfLoop(ListNode pHead)
    {
        if (pHead == null) {
            return null;
        }
        Set<ListNode> set = new HashSet<>();
        ListNode temp_head = pHead;
        while (temp_head != null) {
            if (set.contains(temp_head)) {
                return temp_head;
            }
            set.add(temp_head);
            temp_head = temp_head.next;
        }
        return null;
    }
}

  

剑指Offer——链表中环的入口节点

原文:https://www.cnblogs.com/BaoZiY/p/11183560.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!