deno.land / x / masx200_leetcode_test@10.6.5 / swap-nodes-in-pairs / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { ListNode } from "../reverse-linked-list/ListNode.ts";
export default function swapPairs(head: ListNode | null): ListNode | null { if (!head) { return null; } const res = new ListNode(0, head); let pre: ListNode | undefined | null = res; let cur: ListNode | undefined | null = pre?.next; let next: ListNode | undefined | null = cur?.next; while (pre && cur && next) { cur.next = next.next; next.next = cur; pre.next = next; pre = cur;
cur = pre?.next; next = cur?.next; } return res.next;}
masx200_leetcode_test

Version Info

Tagged at
a year ago