题目

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. Python开发环境Wing IDE如何使用调试功能

    在使用Wing IDE开始调试的时候,需要设置断点的行,读取GetItemCount函数的返回.这可以通过单击行并选择Break工具栏条目,或通过单击行左边的黑色边缘.断点应该以实心红圈的形式出现: ...

  2. 【转】This Android SDK requires Android Developer Toolkit version 20.0.0 or above

    本人最近在操作更新ANDROID SDK时出现类似于题目中的错误,是一启动ECLIPSE时.但是,我现在只是想恢复到原先的开发环境.于是找到本文,方法有效!!! windows 下面安装Android ...

  3. 条件注解@Conditional

    通过活动的profile,可以获得不同的Bean.Spring4提供了一个更通用的基于条件的Bean的创建,即使用@Conditonal注解 @Conditional根据满足某一个特定条件创建一个特定 ...

  4. Mono for Android 设计器错误:Disconnected from layout renderer

        今早打开vs2012 android 项目的时候出现如下错误提示:     查了半天,终于在官方网站得到答案.(http://forums.xamarin.com/discussion/143 ...

  5. extends 继承

    继承的作用:子类可以直接拥有父类成员:其中,私有成员和构造函数不参与继承: java中类继承的特点:只支持单一继承和多重继承,不支持多继承(一个类不能同时继承多个类) 继承中成员变量的特点:子类中可以 ...

  6. Merge更新同步一个表

    merge T2   --目标表using T1    --源表 on T1.id=T2.id   --匹配条件 when matched then    --匹配update set [name]= ...

  7. GitLab一个非标准的端口远程仓库导致clone失败

    首先看下报错信息 当gitlab服务器ssh端口不是默认的22时,使用ssh连接gitlab会出现上面的错误 解决方法: 修改/etc/gitlab/gitlab.rd gitlab_rails['g ...

  8. Linux让Apache支持中文URL图片/文件名

    需要用到iconv_hook和mod_encoding Apache(32位): 安装环境:CentOS 5.6 + Apache 2.2.15 (Apache2.4同样适用) 安装结果:安装后支持“ ...

  9. LeetCode ZigZag Conversion(将字符串排成z字型)

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

  10. Hybris UI的Route(路由)实现

    登录Hybris前台,在product catalog里选择Digital camera: 点击某个产品进入明细页面: 注意产品明细这个url: 这个明细页面的路由和SAP UI5的路由思路很像. 在 ...