题目

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.

代码

/**
* 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 dummyLess(-), dummyGreaterOrEqual(-);
ListNode *p1 = &dummyLess, *p2 = &dummyGreaterOrEqual, *p = head;
while(p){
if ( p->val < x){
p1->next = p;
p1 = p1->next;
}
else{
p2->next = p;
p2 = p2->next;
}
p = p->next;
}
p1->next = dummyGreaterOrEqual.next;
p2->next = NULL;
return dummyLess.next;
}
};

Tips:

链表的基础操作。

设立两个虚表头(代表两个子链表),分别往后接小于x的和大于等于x的,然后再把两个子链表接起来。

==================================================

第二次过这道题,稍微想了一下,想到了两个虚表头的思路;代码一次AC。

/**
* 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 h1();
ListNode h2();
ListNode* p1 = &h1;
ListNode* p2 = &h2;
ListNode* p = head;
while (p)
{
if ( p->val<x )
{
p1->next = new ListNode(p->val);
p1 = p1->next;
}
else
{
p2->next = new ListNode(p->val);
p2 = p2->next;
}
p = p->next;
}
p1->next = h2.next;
return h1.next;
}
};

【Partition List】cpp的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  3. 【Palindrome Partitioning】cpp

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

  4. leetcode 【 Partition List 】python 实现

    题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...

  5. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  6. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  7. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  8. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  9. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

随机推荐

  1. koa源码分析

    最近项目要使用koa,所以提前学习一下,顺便看了koa框架的源码. 注:源码是koa2.x koa的源码很简洁,关键代码只有4个文件,当然还包括一些依赖npm包 const Koa = require ...

  2. nmap -sT -A --script=smb-check-vulns -PO 172.16.21.170

    nmap -sT -A --script=smb-check-vulns -PO 172.16.21.170 调用了nmap的插件--script=smb-check-vulns -sT 隐蔽的tcp ...

  3. 禁止ASP.NET MVC模型绑定时将空字符串绑定为null

    为model添加[DisplayFormat(ConvertEmptyStringToNull = false)] [Display(ResourceType = typeof(AppStrings) ...

  4. 关于Windows创建进程的过程

    之前有听到别人的面试题是问系统创建进程的具体过程是什么,首先想到的是CreateProcess,但是对于具体过程却不是很清楚,今天整理一下. 从操作系统的角度来说 创建进程步骤:        1.申 ...

  5. java基础——类加载与反射

    第1章 类加载器 1.1 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. (1)加载 就是指将class文件读入内存,并为之创 ...

  6. Mybatis自查询递归查找子

    先看一下数据库 主键id,名称product_code,父parent,和kind 设计菜单类 setter,getter Dao public interface ProductMapper { L ...

  7. https及其背后的加密原理阅读总结

    https是以安全为目标的http通道,简单讲是http的安全版.当我们往服务器发送比较隐私的数据(比如说你的银行卡,身份证)时,如果使用http进行通信.那么安全性将得不到保障. 首先数据在传输的过 ...

  8. window10启用administrator 和启用组策略编辑器

    1,启用administrator账户 net user administrator /active:yes 2,启用组策略编辑器    新建一个文本文件.把下面代码粘贴进去.修改后缀名为.cmd  ...

  9. WebViewJavaScriptBridge的原理解析

    理解WebViewJavaScriptBridge原理 前提条件都是需要bridge在OC实例化,然后二者的互调才可以进行下去 _bridge = [WebViewJavascriptBridge b ...

  10. 自动化测试 ubuntu多设备连接不识别

    环境: ubuntu系统 usb2.0 16个口集线器 遇到问题: 连接手机到第11台设备时出现adb devices不显示的现象导致无法通过adb操作 问题排除思路; 1.通过dmesg查看设备连接 ...