【leetcode】Partition List(middle)
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.
思路:先分成大于等于x 和 小于x 两个链表 再连起来 还是用伪头部
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
ListNode large(), small();
ListNode * l = &large;
ListNode * s = &small;
while(head != NULL)
{
if(head->val < x)
{
s = s->next = head;
}
else
{
l = l->next = head;
}
head = head->next;
}
l->next = NULL;
s->next = large.next;
return small.next;
}
};
【leetcode】Partition List(middle)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
随机推荐
- Tomcat 6 —— Realm域管理
本篇来源于官方文档,但不仅仅是翻译,其中不乏网上搜索的资料与自己的理解. 如有错误,请予指正. 什么是Realm 首先说一下什么是Realm,可以把它理解成“域”,也可以理解成“组”,因为它类似 类U ...
- 清北暑假模拟day2 之
/* 现场代码,枚举每条边删除 */ #include<iostream> #include<cstdio> #include<string> #include&l ...
- Net上传附件大小控控值(转)
Server Error 404 – File or directory not found. The resource you are looking for might have been rem ...
- redhat 下 rpm 指令
1.如何安装rpm软件包rmp软件包的安装可以使用程序rpm来完成.执行下面的命令 rpm -i your-package.rpm 其中your-package.rpm是你要安装的rpm包的文件名,一 ...
- 全键盘Vimium快捷键学习记录
0.设置而 vimium 的默认搜索引擎: http://www.baidu.com/s?wd= j: 向下细微滚动窗口. k:向上细微滚动窗口. gg:跳转到页面的顶部.G:跳转到页面的底部.r: ...
- 【C语言入门教程】2.3 整型数据
没有小数位或指数的数据类型被称为整型数据,根据使用方法的分类,整型数据可分为整型常量和整型变量.根据定义或显示的数制分类,可分为十进制.八进制和十六进制. 2.3.1 整型常量 整型常量是在运算中不可 ...
- linux 输出重定向一份到本地文件,屏幕继续输出
ls -al 2>&1 | tee xLog
- C#操作word模板插入文字、图片及表格详细步骤
c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...
- NoSuchMethodException问题总结
1.编译异常,这个很容易发现并解决: method真的没有 替换jar包没有clean project. 2.编译正常,运行报错 这是一个遇到之后让人纳闷的异常,脑袋不转弯的时候真的容易被卡住.这时只 ...
- 几个简单实用的css效果
1.要使按钮具有3D效果,只要将它的左上部分边框设置为浅色,右下部分边框设置为深色即可. eg:#button { background: #888; border: 2px solid; borde ...