Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
leetcode: finished #143
Browse files Browse the repository at this point in the history
  • Loading branch information
xqm32 committed Feb 22, 2024
1 parent 8066379 commit 82d4803
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions content/leetcode/2024/2.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,49 @@ title: 2024.2
draft: false
---

# 2024.2.22

```typescript
/*
* @lc app=leetcode.cn id=143 lang=typescript
*
* [143] 重排链表
*/

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;
}
}

// @lc code=start
function reorderList(head: ListNode | null): void {
if (head === null) return;

const list: ListNode[] = [];
while (head !== null) {
list.push(head);
head = head.next;
}

for (let i = 0, j = list.length - 1; i < j; i++, j--) {
list[i].next = list[j];
if (i + 1 < j - 1) {
list[j].next = list[i + 1];
} else if (i + 1 === j - 1) {
list[j].next = list[i + 1];
list[i + 1].next = null;
} else if (i + 1 === j) {
list[j].next = null;
}
}
}
// @lc code=end
```

# 2024.2.21

```python
Expand Down

0 comments on commit 82d4803

Please sign in to comment.