题目:

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. redis远程登录

    #redis-cli -h 202.96.126.37 -p 6379 #auth 'd6d72fa9b2ff458e:GRjZmQ3MTN'

  2. IOS研究之网络编程(二)-Cocoa Streams使用具体解释

     本文以及相关的系列文章是我总结的iOS网络开发方面的知识点,本文是第二篇,主要分析了Cocoa Streams中的几个重要类 Cocoa Streams实际上是Objective-C对CFNet ...

  3. 2014年java软件project师面试题收集

    如果页面中于五个li标签.写个js使点击每个li返回他的index <!doctype html> <html> <head> <style> li{c ...

  4. 关于python webpy的request

    获取get值i = web.input()keyword = i.get('keyword') 判断get值是否存在if i.has_key('keyword')

  5. AOS应用基础平台-模块开发流程

    AOS平台简单介绍 AOS应用基础平台基于JavaEE技术体系,以"标准功能可复用.通用模块可配置.行业需求高速开发.异构系统无缝集成"为目标.为软件开发团队提供高效可控.随需应变 ...

  6. windows下MySQL 5.7+ 解压缩版安装配置方法(转,写的很简单精辟 赞)

    方法来自伟大的互联网. 1.去官网下载.zip格式的MySQL Server的压缩包,根据需要选择x86或x64版.注意:下载是需要注册账户并登录的. 2.解压缩至你想要的位置. 3.复制解压目录下m ...

  7. npm 全局配置放在c盘/用户/当前用户/目录下

    prefix=D:\Users\Ed\AppData\Roaming\nodejs\npm-globalcache=D:\Users\Ed\AppData\Roaming\npm-cacheregis ...

  8. Notepad工具使用小技巧

    工欲善其事必先利其器 Notepad++是个很不错的文本编辑工具,掌握它的使用技巧可以提高我们工作的效率.见如下: 比较常用的罗列如下:(如果有更好的建议可以留言哈) 1: 添加书签 CTRL+F2 ...

  9. 经常遇到js的面试题

    大家都知道在面试的时候,很多前端的必须要问的就是js的问题,最近我们公司也有很多这样的面试,我提了一些个问题,还有我面试的时候面试官面试我的问题汇总,也有百度的别人的,希望对那些刚进入这个行业的有一些 ...

  10. 【剑指Offer学习】【面试题33:把数组排成最小的数】

    题目:输入一个正整数数组,把数组里全部数字拼接起来排成一个数.打印能拼接出的全部数字中最小的一个. 样例说明: 比如输入数组{3. 32, 321},则扫描输出这3 个数字能排成的最小数字321323 ...