(剑指offer)链表中环的入口节点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ccnuacmhdu/article/details/84889105

时间限制:1秒 空间限制:32768K 热度指数:132023
本题知识点: 链表

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

/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
import java.util.*;
public class Solution {

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

猜你喜欢

转载自blog.csdn.net/ccnuacmhdu/article/details/84889105