题解 | #链表中环的入口结点#
链表中环的入口结点
http://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
/** * 用hashMap保存下个节点的前驱节点,当key有多个前驱节点就是入口 * * @param pHead * @return */ public static ListNode EntryNodeOfLoop(ListNode pHead) { Map<ListNode, ListNode> map = new HashMap<>(); if (pHead == null || pHead.next == null) { return null; } ListNode next = pHead; while (next != null) { if (map.get(next.next) == null) { map.put(next.next, next); } else { if (next != pHead) { return next.next; } else { return next; } } next = next.next; } return null; }
算法 文章被收录于专栏
数据结构和算法