We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 471cb16 commit 99bd132Copy full SHA for 99bd132
linked-list-cycle/hyer0705.ts
@@ -0,0 +1,25 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * class ListNode {
4
+ * val: number
5
+ * next: ListNode | null
6
+ * constructor(val?: number, next?: ListNode | null) {
7
+ * this.val = (val===undefined ? 0 : val)
8
+ * this.next = (next===undefined ? null : next)
9
+ * }
10
11
+ */
12
+
13
+function hasCycle(head: ListNode | null): boolean {
14
+ if (!head || !head.next) return false;
15
16
+ let slow = head;
17
+ let fast = head;
18
+ while (fast && fast.next) {
19
+ slow = slow.next;
20
+ fast = fast.next.next;
21
+ if (slow === fast) return true;
22
+ }
23
24
+ return false;
25
+}
0 commit comments