加载中...
86-分隔链表(Partition List)
发表于:2021-12-03 | 分类: 中等
字数统计: 247 | 阅读时长: 1分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/partition-list

英文原文

Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

 

Example 1:

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

Example 2:

Input: head = [2,1], x = 2
Output: [1,2]

 

Constraints:

  • The number of nodes in the list is in the range [0, 200].
  • -100 <= Node.val <= 100
  • -200 <= x <= 200

中文题目

给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

你应当 保留 两个分区中每个节点的初始相对位置。

 

示例 1:

输入:head = [1,4,3,2,5,2], x = 3
输出:[1,2,2,4,3,5]

示例 2:

输入:head = [2,1], x = 2
输出:[1,2]

 

提示:

  • 链表中节点的数目在范围 [0, 200]
  • -100 <= Node.val <= 100
  • -200 <= x <= 200

通过代码

高赞题解

方法一:模拟

直观来说我们只需维护两个链表 $\textit{small}$ 和 $\textit{large}$ 即可,$\textit{small}$ 链表按顺序存储所有小于 $x$ 的节点,$\textit{large}$ 链表按顺序存储所有大于等于 $x$ 的节点。遍历完原链表后,我们只要将 $\textit{small}$ 链表尾节点指向 $\textit{large}$ 链表的头节点即能完成对链表的分隔。

为了实现上述思路,我们设 $\textit{smallHead}$ 和 $\textit{largeHead}$ 分别为两个链表的哑节点,即它们的 $\textit{next}$ 指针指向链表的头节点,这样做的目的是为了更方便地处理头节点为空的边界条件。同时设 $\textit{small}$ 和 $\textit{large}$ 节点指向当前链表的末尾节点。开始时 $\textit{smallHead}=\textit{small},\textit{largeHead}=\textit{large}$。随后,从前往后遍历链表,判断当前链表的节点值是否小于 $x$,如果小于就将 $\textit{small}$ 的 $\textit{next}$ 指针指向该节点,否则将 $\textit{large}$ 的 $\textit{next}$ 指针指向该节点。

遍历结束后,我们将 $\textit{large}$ 的 $\textit{next}$ 指针置空,这是因为当前节点复用的是原链表的节点,而其 $\textit{next}$ 指针可能指向一个小于 $x$ 的节点,我们需要切断这个引用。同时将 $\textit{small}$ 的 $\textit{next}$ 指针指向 $\textit{largeHead}$ 的 $\textit{next}$ 指针指向的节点,即真正意义上的 $\textit{large}$ 链表的头节点。最后返回 $\textit{smallHead}$ 的 $\textit{next}$ 指针即为我们要求的答案。

[sol1-C++]
class Solution { public: ListNode* partition(ListNode* head, int x) { ListNode* small = new ListNode(0); ListNode* smallHead = small; ListNode* large = new ListNode(0); ListNode* largeHead = large; while (head != nullptr) { if (head->val < x) { small->next = head; small = small->next; } else { large->next = head; large = large->next; } head = head->next; } large->next = nullptr; small->next = largeHead->next; return smallHead->next; } };
[sol1-Java]
class Solution { public ListNode partition(ListNode head, int x) { ListNode small = new ListNode(0); ListNode smallHead = small; ListNode large = new ListNode(0); ListNode largeHead = large; while (head != null) { if (head.val < x) { small.next = head; small = small.next; } else { large.next = head; large = large.next; } head = head.next; } large.next = null; small.next = largeHead.next; return smallHead.next; } }
[sol1-JavaScript]
var partition = function(head, x) { let small = new ListNode(0); const smallHead = small; let large = new ListNode(0); const largeHead = large; while (head !== null) { if (head.val < x) { small.next = head; small = small.next; } else { large.next = head; large = large.next; } head = head.next; } large.next = null; small.next = largeHead.next; return smallHead.next; };
[sol1-Golang]
func partition(head *ListNode, x int) *ListNode { small := &ListNode{} smallHead := small large := &ListNode{} largeHead := large for head != nil { if head.Val < x { small.Next = head small = small.Next } else { large.Next = head large = large.Next } head = head.Next } large.Next = nil small.Next = largeHead.Next return smallHead.Next }
[sol1-C]
struct ListNode* partition(struct ListNode* head, int x) { struct ListNode* small = malloc(sizeof(struct ListNode)); struct ListNode* smallHead = small; struct ListNode* large = malloc(sizeof(struct ListNode)); struct ListNode* largeHead = large; while (head != NULL) { if (head->val < x) { small->next = head; small = small->next; } else { large->next = head; large = large->next; } head = head->next; } large->next = NULL; small->next = largeHead->next; return smallHead->next; }

复杂度分析

  • 时间复杂度: $O(n)$,其中 $n$ 是原链表的长度。我们对该链表进行了一次遍历。

  • 空间复杂度: $O(1)$。

统计信息

通过次数 提交次数 AC比率
127053 200850 63.3%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
85-最大矩形(Maximal Rectangle)
下一篇:
87-扰乱字符串(Scramble String)
本文目录
本文目录