diff --git a/solution/0100-0199/0143.Reorder List/README.md b/solution/0100-0199/0143.Reorder List/README.md index 5adb2d6e57d60..3fcbdac31e0ef 100644 --- a/solution/0100-0199/0143.Reorder List/README.md +++ b/solution/0100-0199/0143.Reorder List/README.md @@ -248,20 +248,20 @@ function reorderList(head: ListNode | null): void { let slow = head; let fast = head; // 找到中心节点 - while (fast != null && fast.next != null) { + while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } // 反转节点 let next = slow.next; slow.next = null; - while (next != null) { + while (next) { [next.next, slow, next] = [slow, next, next.next]; } // 合并 let left = head; let right = slow; - while (right.next != null) { + while (right.next) { const next = left.next; left.next = right; right = right.next; diff --git a/solution/0100-0199/0143.Reorder List/README_EN.md b/solution/0100-0199/0143.Reorder List/README_EN.md index 03652c7afd7ab..19343515d07a7 100644 --- a/solution/0100-0199/0143.Reorder List/README_EN.md +++ b/solution/0100-0199/0143.Reorder List/README_EN.md @@ -222,20 +222,20 @@ func reorderList(head *ListNode) { function reorderList(head: ListNode | null): void { let slow = head; let fast = head; - while (fast != null && fast.next != null) { + while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } let next = slow.next; slow.next = null; - while (next != null) { + while (next) { [next.next, slow, next] = [slow, next, next.next]; } let left = head; let right = slow; - while (right.next != null) { + while (right.next) { const next = left.next; left.next = right; right = right.next; diff --git a/solution/0100-0199/0143.Reorder List/Solution.ts b/solution/0100-0199/0143.Reorder List/Solution.ts index e38b213aeb7d1..d560e9551aef5 100644 --- a/solution/0100-0199/0143.Reorder List/Solution.ts +++ b/solution/0100-0199/0143.Reorder List/Solution.ts @@ -14,19 +14,26 @@ Do not return anything, modify head in-place instead. */ function reorderList(head: ListNode | null): void { - const arr = []; - let node = head; - while (node.next != null) { - arr.push(node); - node = node.next; + let slow = head; + let fast = head; + while (fast && fast.next) { + slow = slow.next; + fast = fast.next.next; } - let l = 0; - let r = arr.length - 1; - while (l < r) { - const start = arr[l]; - const end = arr[r]; - [end.next.next, start.next, end.next] = [start.next, end.next, null]; - l++; - r--; + + let next = slow.next; + slow.next = null; + while (next) { + [next.next, slow, next] = [slow, next, next.next]; + } + + let left = head; + let right = slow; + while (right.next) { + const next = left.next; + left.next = right; + right = right.next; + left.next.next = next; + left = left.next.next; } } diff --git a/solution/0100-0199/0143.Reorder List/Solution2.ts b/solution/0100-0199/0143.Reorder List/Solution2.ts deleted file mode 100644 index b33a007eff343..0000000000000 --- a/solution/0100-0199/0143.Reorder List/Solution2.ts +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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) - * } - * } - */ - -/** - Do not return anything, modify head in-place instead. - */ -function reorderList(head: ListNode | null): void { - let slow = head; - let fast = head; - while (fast != null && fast.next != null) { - slow = slow.next; - fast = fast.next.next; - } - - let next = slow.next; - slow.next = null; - while (next != null) { - [next.next, slow, next] = [slow, next, next.next]; - } - - let left = head; - let right = slow; - while (right.next != null) { - const next = left.next; - left.next = right; - right = right.next; - left.next.next = next; - left = left.next.next; - } -}