142.环形链表-ii
小于 1 分钟
142.环形链表-ii
/*
* @lc app=leetcode.cn id=142 lang=typescript
*
* [142] 环形链表 II
*/
// @lc code=start
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function detectCycle(head: ListNode | null): ListNode | null {
if (head == null) return null;
let map: Map<ListNode, string> = new Map();
let current: ListNode | null = head;
while (current.next) {
if (map.has(current)) {
// 存在
return current;
}
map.set(current, "1");
current = current.next;
}
return null
};
// @lc code=end
Loading...
