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. linux Crontab定时备份项目案例

    首先先写好备份的脚本(拷贝的命令) #bash/bin cd /finance/tomcat8-finance/wtpwebapps tar -czf /finance/webapp_backup/* ...

  2. java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.

    java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more tha ...

  3. JS获取访客IP进行自动跳转

    因业务需要进行地区判断跳转指定站点,下面是我个人实现的办法,分享给大家,仅供参考,切勿做非法用途 第一步,获取IP并判断归属地 直接使用搜狐的IP库查询接口 <script type=" ...

  4. junit4的初级用法

    junit4初级用法: 一:各个标签的意思 1.@Test用来标注测试函数 2.@Before用来标注此函数在每次测试函数运行之前运行(每执行一个@Test之前都要运行一遍@Before) 3.@Af ...

  5. mysql 查询账户

    查询 mysql 的存在的账户  >select user,host,password from mysql.user; # 可以查询涉及到user. host 链接权限.密码加密文件.

  6. AC自动机入门经典题目(两种表达方式)

    Keywords Search 指针方式: /* Keywords Search */ # include <iostream> # include <stdio.h> # i ...

  7. javascript加密之md5加密

    原作地址:JavaScriptMd5 修改备用:JavaScriptMd5.rar

  8. mysql 获取数学成绩最高以及最低的同学

    mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...

  9. mysql e的n次幂exp()

    mysql> ); +-------------------+ | exp() | +-------------------+ | 2.718281828459045 | +---------- ...

  10. 记录linux上mongo迁移使用的命令

    首先mongodb的文件路径必须在系统盘,这里是 这里安装路径 /usr/mongodb/bin 一般迁移的只是db文件夹和log文件 看配置文件内容 port=27017 #端口 dbpath=/d ...