题目

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. Android 应用开机自启和无需权限开启悬浮框

    开机自启主要自定义广播接收类,且需要在清单文件中注册,不要在代码中动态注册. <uses-permission android:name="android.permission.REC ...

  2. Thread.sleep 与Thread.currentThread.sleep

    参考博客: https://blog.csdn.net/guangyinglanshan/article/details/51645053 公司项目近段时间要使用thread, 个人想去了解Threa ...

  3. php 实现格式化数字功能

    php 实现数字格式化功能 /** * @param $num 数字 * @param int $decimal 精度 * @param int $point_len 分隔位长度 * @return ...

  4. Citrix-Netscaler-VPX-11.0

    平台: CentOS 类型: 虚拟机镜像 软件包: Citrix linux basic software Loadbalance security waf wlb 服务优惠价: 按服务商许可协议 云 ...

  5. 【BZOJ3930】[CQOI2015] 选数(容斥)

    点此看题面 大致题意: 让你求出在区间\([L,H]\)间选择\(n\)个数时,有多少种方案使其\(gcd\)为\(K\). 容斥 原以为是一道可怕的莫比乌斯反演题. 但是,数据范围中有这样一句话:\ ...

  6. python_71_json序列化1

    #序列化:序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程. #本例把字典数据类型存成字符串存在硬盘 #文件只能存字符串和二进制码,字典之类的不可以 info={ ...

  7. SQLAlchemy简介

    一.SQLAlchemy简介 SQLAlchemy是Python SQL工具包和对象关系映射器,是python中最著名的ORM(Object Relationship Mapping)框架,它简化了应 ...

  8. WinSCP使用与linux命令(小部分命令)

    一.下载一个WinSCP WinSCP是一个Windows环境下使用SSH的开源图形化SFTP客户端.同时支持SCP协议.它的主要功能就是在本地与远程计算机间安全的复制文件..winscp也可以链接其 ...

  9. STL笔记(こ)--删除数组中重复元素

    使用STL中的Unique函数: #include<bits/stdc++.h> using namespace std; void fun(int &n) //配套for_eac ...

  10. 爬虫学习(四)——post请求爬取

    百度翻译爬取数据 import urllib.requestimport urllib.parsepost_url = "https://fanyi.baidu.com/sug"h ...