Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solutions to lc problem: No.0143 #2547

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 21 additions & 62 deletions solution/0100-0199/0143.Reorder List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,28 @@ func reorderList(head *ListNode) {
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 != null && fast.next != null) {
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 != 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;
}
}
```
Expand Down Expand Up @@ -399,53 +407,4 @@ public class Solution {

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```ts
/**
* 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;
}
}
```

<!-- tabs:end -->

<!-- end -->
118 changes: 25 additions & 93 deletions solution/0100-0199/0143.Reorder List/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ L<sub>0</sub> &rarr; L<sub>n</sub> &rarr; L<sub>1</sub> &rarr; L<sub>n - 1</sub>

## Solutions

### Solution 1
### Solution 1: Fast and Slow Pointers + Reverse List + Merge Lists

We first use fast and slow pointers to find the midpoint of the linked list, then reverse the second half of the list, and finally merge the two halves.

The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -57,26 +61,21 @@ L<sub>0</sub> &rarr; L<sub>n</sub> &rarr; L<sub>1</sub> &rarr; L<sub>n - 1</sub>
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
# 快慢指针找到链表中点
fast = slow = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next

# cur 指向右半部分链表
cur = slow.next
slow.next = None

# 反转右半部分链表
pre = None
while cur:
t = cur.next
cur.next = pre
pre, cur = cur, t
cur = head

# 此时 cur, pre 分别指向链表左右两半的第一个节点
# 合并
while pre:
t = pre.next
pre.next = cur.next
Expand All @@ -97,18 +96,15 @@ class Solution:
*/
class Solution {
public void reorderList(ListNode head) {
// 快慢指针找到链表中点
ListNode fast = head, slow = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}

// cur 指向右半部分链表
ListNode cur = slow.next;
slow.next = null;

// 反转右半部分链表
ListNode pre = null;
while (cur != null) {
ListNode t = cur.next;
Expand All @@ -118,8 +114,6 @@ class Solution {
}
cur = head;

// 此时 cur, pre 分别指向链表左右两半的第一个节点
// 合并
while (pre != null) {
ListNode t = pre.next;
pre.next = cur.next;
Expand All @@ -145,19 +139,16 @@ class Solution {
class Solution {
public:
void reorderList(ListNode* head) {
// 快慢指针找到链表中点
ListNode* fast = head;
ListNode* slow = head;
while (fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}

// cur 指向右半部分链表
ListNode* cur = slow->next;
slow->next = nullptr;

// 反转右半部分链表
ListNode* pre = nullptr;
while (cur) {
ListNode* t = cur->next;
Expand All @@ -167,8 +158,6 @@ public:
}
cur = head;

// 此时 cur, pre 分别指向链表左右两半的第一个节点
// 合并
while (pre) {
ListNode* t = pre->next;
pre->next = cur->next;
Expand All @@ -189,17 +178,14 @@ public:
* }
*/
func reorderList(head *ListNode) {
// 快慢指针找到链表中点
fast, slow := head, head
for fast.Next != nil && fast.Next.Next != nil {
slow, fast = slow.Next, fast.Next.Next
}

// cur 指向右半部分链表
cur := slow.Next
slow.Next = nil

// 反转右半部分链表
var pre *ListNode
for cur != nil {
t := cur.Next
Expand All @@ -208,8 +194,6 @@ func reorderList(head *ListNode) {
}
cur = head

// 此时 cur, pre 分别指向链表左右两半的第一个节点
// 合并
for pre != nil {
t := pre.Next
pre.Next = cur.Next
Expand All @@ -236,20 +220,27 @@ func reorderList(head *ListNode) {
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 != null && fast.next != null) {
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 != 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;
}
}
```
Expand Down Expand Up @@ -305,19 +296,16 @@ impl Solution {
* @return {void} Do not return anything, modify head in-place instead.
*/
var reorderList = function (head) {
// 快慢指针找到链表中点
let slow = head;
let fast = head;
while (fast.next && fast.next.next) {
slow = slow.next;
fast = fast.next.next;
}

// cur 指向右半部分链表
let cur = slow.next;
slow.next = null;

// 反转右半部分链表
let pre = null;
while (cur) {
const t = cur.next;
Expand All @@ -327,8 +315,6 @@ var reorderList = function (head) {
}
cur = head;

// 此时 cur, pre 分别指向链表左右两半的第一个节点
// 合并
while (pre) {
const t = pre.next;
pre.next = cur.next;
Expand All @@ -353,19 +339,16 @@ var reorderList = function (head) {
*/
public class Solution {
public void ReorderList(ListNode head) {
// 快慢指针找到链表中点
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}

// cur 指向右半部分链表
ListNode cur = slow.next;
slow.next = null;

// 反转右半部分链表
ListNode pre = null;
while (cur != null) {
ListNode t = cur.next;
Expand All @@ -375,8 +358,6 @@ public class Solution {
}
cur = head;

// 此时 cur, pre 分别指向链表左右两半的第一个节点
// 合并
while (pre != null) {
ListNode t = pre.next;
pre.next = cur.next;
Expand All @@ -390,53 +371,4 @@ public class Solution {

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```ts
/**
* 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;
}
}
```

<!-- tabs:end -->

<!-- end -->
Loading
Loading