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.

Example:

Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5

给一个链表和一个x值,划分链表,把所有小于x值的节点都移到大于等于x值的前面,两部分的节点顺序不变。

解法:维护两个queue,一个存小于x值的,一个存大于等于x值的,最后在把两个连在一起。记得把大于等于的最后加上null。

Java:

public ListNode partition(ListNode head, int x) {
ListNode dummy1 = new ListNode(0), dummy2 = new ListNode(0); //dummy heads of the 1st and 2nd queues
ListNode curr1 = dummy1, curr2 = dummy2; //current tails of the two queues;
while (head!=null){
if (head.val<x) {
curr1.next = head;
curr1 = head;
}else {
curr2.next = head;
curr2 = head;
}
head = head.next;
}
curr2.next = null; //important! avoid cycle in linked list. otherwise u will get TLE.
curr1.next = dummy2.next;
return dummy1.next;
}

Python:

# Time:  O(n)
# Space: O(1)
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None def __repr__(self):
if self:
return "{} -> {}".format(self.val, repr(self.next)) class Solution(object):
# @param head, a ListNode
# @param x, an integer
# @return a ListNode
def partition(self, head, x):
dummySmaller, dummyGreater = ListNode(-1), ListNode(-1)
smaller, greater = dummySmaller, dummyGreater while head:
if head.val < x:
smaller.next = head
smaller = smaller.next
else:
greater.next = head
greater = greater.next
head = head.next smaller.next = dummyGreater.next
greater.next = None return dummySmaller.next

C++:

// Time:  O(n)
// Space: O(1)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
ListNode dummy_smaller{0};
ListNode dummy_larger{0};
auto smaller = &dummy_smaller;
auto larger = &dummy_larger; while (head) {
if (head->val < x) {
smaller->next = head;
smaller = smaller->next;
} else {
larger->next = head;
larger = larger->next;
}
head = head->next;
}
smaller->next = dummy_larger.next;
larger->next = nullptr; return dummy_smaller.next;
}
};

C++:

ListNode *partition(ListNode *head, int x) {
ListNode node1(0), node2(0);
ListNode *p1 = &node1, *p2 = &node2;
while (head) {
if (head->val < x)
p1 = p1->next = head;
else
p2 = p2->next = head;
head = head->next;
}
p2->next = NULL;
p1->next = node2.next;
return node1.next;
}

  

All LeetCode Questions List 题目汇总

[LeetCode] 86. Partition List 划分链表的更多相关文章

  1. LeetCode 86. Partition List 划分链表 C++

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  2. [leetcode]86. Partition List划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  3. [LeetCode]86. Partition List分离链表

    /* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...

  4. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  5. [CareerCup] 2.4 Partition List 划分链表

    2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...

  6. [LeetCode] Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  7. leetcode 86. Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  8. partition List(划分链表)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  9. [LeetCode] 86. Partition List 解题思路

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

随机推荐

  1. Kotlin协程第一个示例剖析及Kotlin线程使用技巧

    Kotlin协程第一个示例剖析: 上一次https://www.cnblogs.com/webor2006/p/11712521.html已经对Kotlin中的协程有了理论化的了解了,这次则用代码来直 ...

  2. 数组函数some、every、find、filter、map、forEach有什么区别

  3. Beta冲刺(2/7)——2019.5.23

    所属课程 软件工程1916|W(福州大学) 作业要求 Beta冲刺(2/7)--2019.5.23 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万里 ...

  4. 08.vue-router动态路由匹配

    动态匹配路由的基本用法 思考: <!-有如下3个路由链接.-> <router-link to="/user/1">User1</router-lin ...

  5. Ubuntu 14.04.2升级openssh7.9

    环境:Ubuntu 14.04.2 目的:openssh版本6.6升级为openssh7.9 准备以下3个包 http://www.zlib.net/ https://www.openssl.org/ ...

  6. Centos 6.5出现yum安装慢的情况

    最近在用Centos 6.5 的时候出现了这种情况, Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirro ...

  7. 爬虫 - 请求库之selenium

    介绍 官方文档 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的 ...

  8. Java中String、LocalDateTime、LocalDate、Date互转

    String 转LocalDate和LocalDateTime LocalDate startDate = LocalDate.parse("2019-12-05", DateTi ...

  9. php正则表示中的元字符

    元字符 抛出问题: \d 代表匹配一个字符.而我现在想要匹配十个八个,任意多个数字肿么办? 这个时候我们就要用到元字符.在使用原子的时候,发现只能够匹配一个字符,可是要匹配多个字符就出现了问题.大理石 ...

  10. circus && web comsole docker-compose 独立部署

    问题的根本原因是web console 的bug(实际上还是python 对于依赖版本出来不明确) circus 进程docker 镜像 dockerfile FROM python:slim-str ...