141.环形链表
小于 1 分钟
141.环形链表
/*
* @lc app=leetcode.cn id=141 lang=typescript
*
* [141] 环形链表
*/
// @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 hasCycle(head: ListNode | null): boolean {
if (head == null) return false;
let map: Map<ListNode, string> = new Map();
let current: ListNode | null = head;
while (current.next) {
if (map.has(current)) {
// 存在
return true;
}
map.set(current, "1");
current = current.next;
}
return false
}
// @lc code=end
Loading...
