【Partition List】cpp
题目:
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的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Palindrome Partitioning】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- leetcode 【 Partition List 】python 实现
题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
随机推荐
- 菜鸟 学注册机编写之 “sha1”
1. 首先运行程序随便输入用户与注册码如下图所示: 2.将程序载入OD, 下MessageBoxA函数断点, F9运行程序, 程序运行后随便输入用户名与注册码,点"OK"后断下,F ...
- VS中生成网站和发布网站的区别
VS中生成网站和发布网站的区别 生成网站:是网站项目的编译. 我们知道像一样的C#编译性语言,在运行程序的时候,首先都要经过编译成计算机识别的二进制代码,才能运行.还有网站编译后,浏览 ...
- 一、基于Qt的图像矩形区域改色
Qt环境下图像的打开和涂色 一.设计目标 能够在 Qt QtCreator 环境下打开常用图像格式文件,诸如 bmp.jpg.png 图像等,然后将他们转化为 Qt 中的 QImage 类,并进行矩形 ...
- instanceof 关键字
boolean = Object(类引用名) instanceof Class(类名) 作用:判断符号左边的引用指向的对象是否是右边这个类的对象:
- 工作中碰到的css问题解决方法
好久都没来这写东西了,都长草了.刚解决的两个小问题,先记下来 textarea横向没有滚动条加上 wrap="off"这个属性 英文单词不断行加上这个 word-break:bre ...
- gearmand 编译 could not find gperf
安装步骤: #wget https://launchpad.net/gearmand/1.2/1.1.8/+download/gearmand-1.1.8.tar.gz #tar zxvf gearm ...
- Hybrid App开发之css样式使用
前言: 前面学习了html,今天学习一下css的基本使用,看下html与css之间是如何结合来编写前端网页的. CSS 是什么? CSS 是 Cascading Style Sheets(级联样式表) ...
- 解决wget下载https时报错 --no-check-certificate (不检查证书)
如果使用 wget下载https开头的网址域名 时报错,你需要加上 --no-check-certificate (不检查证书)选项 例如: wget https://pypi.python.org/ ...
- dojo中类的继承
类似于c# java等后台语言,在基于类的面向对象编程中,通常需要在子类中扩展某些父类的方法,这时可以在子类的方法中,先调用从父类继承的方法,然后再执行子类自定义的操作.凡是使用declare创建的类 ...
- C#箴言之用属性来访问类的私有成员
在程序中,难免要访问某个对象的私有成员.那么以前实现这类功能的方法有两种,第一种方法最简单,就是把成员访问符从“private”改为“public”即可:而另一个就是提供公有的成员访问函数来进行访问. ...