deno.land / x / masx200_leetcode_test@10.6.5 / reorder-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
import { ListNode } from "../reverse-linked-list/ListNode.ts";
export default function reorderList(head: ListNode | null): void { if (!head) return;
const nodes: Array<ListNode> = []; let current: ListNode | null = head; while (current) { const next: ListNode | null = current.next; nodes.push(current); current.next = null; current = next; } for (let i = 0; i < nodes.length / 2; i++) { const node: ListNode = nodes[i]; const next: ListNode = nodes[nodes.length - i - 1]; if (node === next) return; node.next = next; next.next = nodes[i + 1]; }
nodes[Math.floor(nodes.length / 2)].next = null;}
masx200_leetcode_test

Version Info

Tagged at
a year ago