Partition List

Question

Problem Statement

Given 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.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

题解

此题出自 CTCI 题 2.4,依据题意,是要根据值x对链表进行分割操作,具体是指将所有小于x的节点放到不小于x的节点之前,咋一看和快速排序的分割有些类似,但是这个题的不同之处在于只要求将小于x的节点放到前面,而并不要求对元素进行排序。

这种分割的题使用两路指针即可轻松解决。左边指针指向小于x的节点,右边指针指向不小于x的节点。由于左右头节点不确定,我们可以使用两个dummy节点。

Python

  1. """
  2. Definition of ListNode
  3. class ListNode(object):
  4. def __init__(self, val, next=None):
  5. self.val = val
  6. self.next = next
  7. """
  8. class Solution:
  9. """
  10. @param head: The first node of linked list.
  11. @param x: an integer
  12. @return: a ListNode
  13. """
  14. def partition(self, head, x):
  15. if head is None:
  16. return None
  17. leftDummy = ListNode(0)
  18. left = leftDummy
  19. rightDummy = ListNode(0)
  20. right = rightDummy
  21. node = head
  22. while node is not None:
  23. if node.val < x:
  24. left.next = node
  25. left = left.next
  26. else:
  27. right.next = node
  28. right = right.next
  29. node = node.next
  30. # post-processing
  31. right.next = None
  32. left.next = rightDummy.next
  33. return leftDummy.next

C++

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* partition(ListNode* head, int x) {
  12. if (head == NULL) return NULL;
  13. ListNode *leftDummy = new ListNode(0);
  14. ListNode *left = leftDummy;
  15. ListNode *rightDummy = new ListNode(0);
  16. ListNode *right = rightDummy;
  17. ListNode *node = head;
  18. while (node != NULL) {
  19. if (node->val < x) {
  20. left->next = node;
  21. left = left->next;
  22. } else {
  23. right->next = node;
  24. right = right->next;
  25. }
  26. node = node->next;
  27. }
  28. // post-processing
  29. right->next = NULL;
  30. left->next = rightDummy->next;
  31. return leftDummy->next;
  32. }
  33. };

Java

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public ListNode partition(ListNode head, int x) {
  11. ListNode leftDummy = new ListNode(0);
  12. ListNode leftCurr = leftDummy;
  13. ListNode rightDummy = new ListNode(0);
  14. ListNode rightCurr = rightDummy;
  15. ListNode runner = head;
  16. while (runner != null) {
  17. if (runner.val < x) {
  18. leftCurr.next = runner;
  19. leftCurr = leftCurr.next;
  20. } else {
  21. rightCurr.next = runner;
  22. rightCurr = rightCurr.next;
  23. }
  24. runner = runner.next;
  25. }
  26. // cut off ListNode after rightCurr to avoid cylic
  27. rightCurr.next = null;
  28. leftCurr.next = rightDummy.next;
  29. return leftDummy.next;
  30. }
  31. }

源码分析

  1. 异常处理
  2. 引入左右两个dummy节点及left和right左右尾指针
  3. 遍历原链表
  4. 处理右链表,置right->next为空(否则如果不为尾节点则会报错,处理链表时 以 null 为判断),将右链表的头部链接到左链表尾指针的next,返回左链表的头部

复杂度分析

遍历链表一次,时间复杂度近似为 O(n), 使用了两个 dummy 节点及中间变量,空间复杂度近似为 O(1).