题目:

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

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

题解:

Solution 1 ()

class Solution {
public:
ListNode *partition(ListNode *head, int x) {
ListNode* left = new ListNode(-);
ListNode* right = new ListNode(-);
ListNode* l = left;
ListNode* r = right;
while (head) {
if (head->val < x) {
l->next = head;
l = l->next;
} else {
r->next = head;
r = r->next;
}
head = head->next;
}
l->next = right->next;
r->next = nullptr; return left->next;
}
};

【Lintcode】096.Partition List的更多相关文章

  1. 【CF932G】Palindrome Partition(回文树,动态规划)

    [CF932G]Palindrome Partition(回文树,动态规划) 题面 CF 翻译: 给定一个串,把串分为偶数段 假设分为了\(s1,s2,s3....sk\) 求,满足\(s_1=s_k ...

  2. 【CF932G】Palindrome Partition 回文自动机

    [CF932G]Palindrome Partition 题意:给你一个字符串s,问你有多少种方式,可以将s分割成k个子串,设k个子串是$x_1x_2...x_k$,满足$x_1=x_k,x_2=x_ ...

  3. 【CF830C】Bamboo Partition 分块

    [CF830C]Bamboo Partition 题解:给你n个数a1,a2...an和k,求最大的d使得$\sum\limits_{i=1}^n((d-a[i] \% d) \% d) \le k$ ...

  4. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  5. 【lintcode】 二分法总结 I

     二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...

  6. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  7. 【原创】leetCodeOj ---Partition List 解题报告

    原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, part ...

  8. 【Leetcode】86. Partition List

    Question: Given a linked list and a value x, partition it such that all nodes less than x come befor ...

  9. 【Lintcode】136.Palindrome Partitioning

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

随机推荐

  1. neural network and deep learning笔记(1)

    neural network and deep learning 这本书看了陆陆续续看了好几遍了,但每次都会有不一样的收获. DL领域的paper日新月异.每天都会有非常多新的idea出来,我想.深入 ...

  2. rtems 4.11 时钟驱动(arm, beagle)

    根据bsp_howto手册,时钟驱动的框架主要在 c/src/lib/libbsp/shared/Clockdrv_shell.h 文件中实现 时钟初始化 时钟驱动初始化函数为 Clock_initi ...

  3. ThinkPHP3.1在多数据库连接下存储过程调用bug修正

    最近使用ThinkPHP3.1进行一个项目的开发,由于该项目需要连接多台不同的数据库,所以使用如下配置方法: <?php return array( //'配置项'=>'配置值' //数据 ...

  4. Docker入门系列8

    commit docker commit -m "Added json gem" -a "Docker Newbee" 0b2616b0e5a8 ouruser ...

  5. HTTP状态码介绍详细

    HTTP协议中几个状态码的含义:1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分, ...

  6. Codeforces Round #316 (Div. 2) (ABC题)

    A - Elections 题意: 每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利. 最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序) ...

  7. Redis源代码分析(十七)--- multi事务操作

    redis作为一非关系型数据库,居然相同拥有与RDBMS的事务操作,不免让我认为比較吃惊.在redis就专门有文件就是运行事务的相关操作的.也能够让我们领略一下.在Redis的代码中是怎样实现事务操作 ...

  8. 基于红帽5裁剪一个简单的Linux

    HOST:宿主机 Target:目标机 1.基于HOST制作一个简单的可启动的Linux 1.给目标磁盘分区 两个: 在宿主机上:/dev/sdb1,/dev/sdb2 /dev/sdb1挂载到 /m ...

  9. python 基础 4.4 生成式 生成器 迭代器

    一.生成式和生成器   列表生成式是python受欢迎的语法之一,通过一句简洁的语法就可以对一组元素进行过滤,还可以对得到的元素进行转换处理.   #/usr/bin/python #coding=u ...

  10. 校验时间冲突SQL写法

    校验时间是否有冲突,时间是就是看两个时间断是否有交集(写法有两种): a.SELECT * FROM xxx WHERE (startTime > a AND startTime < b) ...