deno.land / x / masx200_leetcode_test@10.6.5 / rotate-list / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { ListNode, ListNodeToArray } from "../mod.ts";
function rotateRight(head: ListNode | null, k: number): ListNode | null { if (!head) return null; if (k === 0) return head; if (!head.next) return head; let array = ListNodeToArray(head); const length = array.length; k = k % length; if (k === 0) return head; array = array.map((_, i) => { return array[(i + length - k) % length]; });
let current = head; for (const num of array) { current.val = num; if (!current.next) return head; if (!current || !current.next) { throw Error("current is null"); } current = current.next; } return head;}export default rotateRight;
masx200_leetcode_test

Version Info

Tagged at
a year ago