Skip to content

Commit 81f346e

Browse files
committed
+ problem 143
1 parent e63c7fe commit 81f346e

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 143. Reorder List
2+
You are given the head of a singly linked-list. The list can be represented as:
3+
4+
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
5+
6+
*Reorder the list to be on the following form:*
7+
8+
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
9+
10+
You may not modify the values in the list's nodes. Only nodes themselves may be changed.
11+
12+
#### Example 1:
13+
![](https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg)
14+
<pre>
15+
<strong>Input:</strong> head = [1,2,3,4]
16+
<strong>Output:</strong> [1,4,2,3]
17+
</pre>
18+
19+
#### Example 2:
20+
![](https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg)
21+
<pre>
22+
<strong>Input:</strong> head = [1,2,3,4,5]
23+
<strong>Output:</strong> [1,5,2,4,3]
24+
</pre>
25+
26+
#### Constraints:
27+
* The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.
28+
* `1 <= Node.val <= 1000`
29+
30+
## Solutions (Python)
31+
32+
### 1. Solution
33+
```Python
34+
# Definition for singly-linked list.
35+
# class ListNode:
36+
# def __init__(self, val=0, next=None):
37+
# self.val = val
38+
# self.next = next
39+
class Solution:
40+
def reorderList(self, head: Optional[ListNode]) -> None:
41+
"""
42+
Do not return anything, modify head in-place instead.
43+
"""
44+
length = 0
45+
curr = head
46+
while curr is not None:
47+
length += 1
48+
curr = curr.next
49+
50+
prev = None
51+
curr = head
52+
for _ in range((length + 1) // 2):
53+
prev = curr
54+
curr = curr.next
55+
56+
prev.next = None
57+
prev = None
58+
for _ in range(length // 2):
59+
temp = curr
60+
curr = curr.next
61+
temp.next = prev
62+
prev = temp
63+
64+
curr0 = head
65+
curr1 = prev
66+
for _ in range(length // 2):
67+
prev0 = curr0
68+
prev1 = curr1
69+
curr0 = curr0.next
70+
curr1 = curr1.next
71+
prev0.next = prev1
72+
prev1.next = curr0
73+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 143. 重排链表
2+
给定一个单链表 `L` 的头节点 `head` ,单链表 `L` 表示为:
3+
4+
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
5+
6+
请将其重新排列后变为:
7+
8+
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
9+
10+
不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
11+
12+
#### 示例 1:
13+
![](https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg)
14+
<pre>
15+
<strong>输入:</strong> head = [1,2,3,4]
16+
<strong>输出:</strong> [1,4,2,3]
17+
</pre>
18+
19+
#### 示例 2:
20+
![](https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg)
21+
<pre>
22+
<strong>输入:</strong> head = [1,2,3,4,5]
23+
<strong>输出:</strong> [1,5,2,4,3]
24+
</pre>
25+
26+
#### 提示:
27+
* 链表的长度范围为 <code>[1, 5 * 10<sup>4</sup>]</code>
28+
* `1 <= Node.val <= 1000`
29+
30+
## 题解 (Python)
31+
32+
### 1. 题解
33+
```Python
34+
# Definition for singly-linked list.
35+
# class ListNode:
36+
# def __init__(self, val=0, next=None):
37+
# self.val = val
38+
# self.next = next
39+
class Solution:
40+
def reorderList(self, head: Optional[ListNode]) -> None:
41+
"""
42+
Do not return anything, modify head in-place instead.
43+
"""
44+
length = 0
45+
curr = head
46+
while curr is not None:
47+
length += 1
48+
curr = curr.next
49+
50+
prev = None
51+
curr = head
52+
for _ in range((length + 1) // 2):
53+
prev = curr
54+
curr = curr.next
55+
56+
prev.next = None
57+
prev = None
58+
for _ in range(length // 2):
59+
temp = curr
60+
curr = curr.next
61+
temp.next = prev
62+
prev = temp
63+
64+
curr0 = head
65+
curr1 = prev
66+
for _ in range(length // 2):
67+
prev0 = curr0
68+
prev1 = curr1
69+
curr0 = curr0.next
70+
curr1 = curr1.next
71+
prev0.next = prev1
72+
prev1.next = curr0
73+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def reorderList(self, head: Optional[ListNode]) -> None:
8+
"""
9+
Do not return anything, modify head in-place instead.
10+
"""
11+
length = 0
12+
curr = head
13+
while curr is not None:
14+
length += 1
15+
curr = curr.next
16+
17+
prev = None
18+
curr = head
19+
for _ in range((length + 1) // 2):
20+
prev = curr
21+
curr = curr.next
22+
23+
prev.next = None
24+
prev = None
25+
for _ in range(length // 2):
26+
temp = curr
27+
curr = curr.next
28+
temp.next = prev
29+
prev = temp
30+
31+
curr0 = head
32+
curr1 = prev
33+
for _ in range(length // 2):
34+
prev0 = curr0
35+
prev1 = curr1
36+
curr0 = curr0.next
37+
curr1 = curr1.next
38+
prev0.next = prev1
39+
prev1.next = curr0

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
[139][139l] |[Word Break][139] |![py]&nbsp;&nbsp;![rb]
121121
[141][141l] |[Linked List Cycle][141] |![py]
122122
[142][142l] |[Linked List Cycle II][142] |![py]
123+
[143][143l] |[Reorder List][143] |![py]
123124
[144][144l] |[Binary Tree Preorder Traversal][144] |![py]
124125
[145][145l] |[Binary Tree Postorder Traversal][145] |![py]
125126
[146][146l] |[LRU Cache][146] |![py]
@@ -1651,6 +1652,7 @@
16511652
[139]:Problemset/0139-Word%20Break/README.md#139-word-break
16521653
[141]:Problemset/0141-Linked%20List%20Cycle/README.md#141-linked-list-cycle
16531654
[142]:Problemset/0142-Linked%20List%20Cycle%20II/README.md#142-linked-list-cycle-ii
1655+
[143]:Problemset/0143-Reorder%20List/README.md#143-reorder-list
16541656
[144]:Problemset/0144-Binary%20Tree%20Preorder%20Traversal/README.md#144-binary-tree-preorder-traversal
16551657
[145]:Problemset/0145-Binary%20Tree%20Postorder%20Traversal/README.md#145-binary-tree-postorder-traversal
16561658
[146]:Problemset/0146-LRU%20Cache/README.md#146-lru-cache
@@ -3176,6 +3178,7 @@
31763178
[139l]:https://leetcode.com/problems/word-break/
31773179
[141l]:https://leetcode.com/problems/linked-list-cycle/
31783180
[142l]:https://leetcode.com/problems/linked-list-cycle-ii/
3181+
[143l]:https://leetcode.com/problems/reorder-list/
31793182
[144l]:https://leetcode.com/problems/binary-tree-preorder-traversal/
31803183
[145l]:https://leetcode.com/problems/binary-tree-postorder-traversal/
31813184
[146l]:https://leetcode.com/problems/lru-cache/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
[139][139l] |[单词拆分][139] |![py]&nbsp;&nbsp;![rb]
121121
[141][141l] |[环形链表][141] |![py]
122122
[142][142l] |[环形链表 II][142] |![py]
123+
[143][143l] |[重排链表][143] |![py]
123124
[144][144l] |[二叉树的前序遍历][144] |![py]
124125
[145][145l] |[二叉树的后序遍历][145] |![py]
125126
[146][146l] |[LRU 缓存][146] |![py]
@@ -1651,6 +1652,7 @@
16511652
[139]:Problemset/0139-Word%20Break/README_CN.md#139-单词拆分
16521653
[141]:Problemset/0141-Linked%20List%20Cycle/README_CN.md#141-环形链表
16531654
[142]:Problemset/0142-Linked%20List%20Cycle%20II/README_CN.md#142-环形链表-ii
1655+
[143]:Problemset/0143-Reorder%20List/README_CN.md#143-重排链表
16541656
[144]:Problemset/0144-Binary%20Tree%20Preorder%20Traversal/README_CN.md#144-二叉树的前序遍历
16551657
[145]:Problemset/0145-Binary%20Tree%20Postorder%20Traversal/README_CN.md#145-二叉树的后序遍历
16561658
[146]:Problemset/0146-LRU%20Cache/README_CN.md#146-lru-缓存
@@ -3176,6 +3178,7 @@
31763178
[139l]:https://leetcode.cn/problems/word-break/
31773179
[141l]:https://leetcode.cn/problems/linked-list-cycle/
31783180
[142l]:https://leetcode.cn/problems/linked-list-cycle-ii/
3181+
[143l]:https://leetcode.cn/problems/reorder-list/
31793182
[144l]:https://leetcode.cn/problems/binary-tree-preorder-traversal/
31803183
[145l]:https://leetcode.cn/problems/binary-tree-postorder-traversal/
31813184
[146l]:https://leetcode.cn/problems/lru-cache/

0 commit comments

Comments
 (0)