牛客网 | 链表中环的入口结点

来源:互联网 发布:数据库实用教程答案 编辑:程序博客网 时间:2024/06/05 14:59

一个链表中包含环,请找出该链表的环的入口结点。

/* public class ListNode {    int val;    ListNode next = null;    ListNode(int val) {        this.val = val;    }}*/import java.util.HashSet;public class Solution {    public ListNode EntryNodeOfLoop(ListNode pHead)    {        HashSet<ListNode> set = new HashSet<>();        while(pHead!=null)        {            if(!set.contains(pHead))                set.add(pHead);            else                return pHead;            pHead = pHead.next;        }        return pHead;    }}


0 0
原创粉丝点击