链表中环的入口结点

来源:互联网 发布:java物流管理系统 编辑:程序博客网 时间:2024/05/29 11:47

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

/* public class ListNode {    int val;    ListNode next = null;    ListNode(int val) {        this.val = val;    }}*/import java.util.*;public class Solution {    public static ListNode EntryNodeOfLoop(ListNode pHead)    {        ListNode head = pHead;        HashMap<ListNode, Integer> map = new HashMap<>();        while (head != null) {            if (map.containsKey(head)) return head;            else map.put(head, 1);            head = head.next;        }        return head;    }}
原创粉丝点击