Skip to content

Commit

Permalink
fix: update ts solution
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Apr 7, 2024
1 parent 879cf30 commit 2d25977
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 58 deletions.
6 changes: 3 additions & 3 deletions solution/0100-0199/0143.Reorder List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions solution/0100-0199/0143.Reorder List/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 20 additions & 13 deletions solution/0100-0199/0143.Reorder List/Solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
39 changes: 0 additions & 39 deletions solution/0100-0199/0143.Reorder List/Solution2.ts

This file was deleted.

0 comments on commit 2d25977

Please sign in to comment.