Halo

A magic place for coding

0%

LeetCode解题报告(431)-- 143. Reorder List

Problem

You are given the head of a singly linked-list. The list can be represented as:

1
L0 → L1 → … → Ln - 1 → Ln

Reorder the list to be on the following form:

1
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …

You may not modify the values in the list’s nodes. Only nodes themselves may be changed.

Example 1:

Example1

1
2
Input: head = [1,2,3,4]
Output: [1,4,2,3]

Example 2:

Example2

1
2
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]

Constraints:

  • The number of nodes in the list is in the range [1, 5 * 104].
  • 1 <= Node.val <= 1000

Analysis

  题目给出一个链表,要求调整链表的顺序,第一个元素指向倒数第一个元素,第二个元素指向倒数第二个元素。链表类的题目无非就是操作节点的指针,这里我们先找出规律。如果把每隔一个把元素抽取出来,实际上就是两条链表的合并,一条是从第一个节点到中间的节点,另一条是从最后一个节点到中间的节点。这样一看问题就简化了,两条链表的合并很容易,把原链表切成两部分也很容易,把后半部分翻转一下也很容易,所以这道题目就解决了。实际上就是由三个简单的步骤组合而成:

  1. 中间切分链表;
  2. 翻转后半部分;
  3. 两个链表合并。

Solution

  这里实现有一个细节需要注意,在切分完链表后,对后半部分进行翻转,prev初始化为nullptr,这是因为希望后半部分翻转完之后,结尾能指向空,而不是中间的节点,这是为了便于后续merge的判断条件。


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
// find the middle of the list
ListNode* slow = head, *fast = head;

while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}


// reverse the second half
ListNode* prev = nullptr;
ListNode* cur = slow;
while (cur) {
ListNode* temp = cur->next;
cur->next = prev;
prev = cur;
cur = temp;
}
//slow->next = nullptr;

// merge two list
ListNode* p1 = head, *p2 = prev;
ListNode* temp = nullptr;
while (p2->next) {
temp = p1->next;
p1->next = p2;
p1 = temp;

temp = p2->next;
p2->next = p1;
p2 = temp;
}
}
};

Summary

  这道题第一眼看上去比较复杂,但实际上就是由三个简单的步骤组合而成,并没有新增内容。这道题目的分享到这里,感谢你的支持!

Welcome to my other publishing channels