Partition List leetcode java
题目:
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都放在大于等于x的前面,并且不改变链表之间node原始的相对位置。每次看这道题我老是绕晕,纠结为什么4在3的前面。。其实还是得理解题意,4->3->5都是大于等3的数,而且这保持了他们原来的相对位置 。
所以,这道题是不需要任何排序操作的,题解方法很巧妙。
new两个新链表,一个用来创建所有大于等于x的链表,一个用来创建所有小于x的链表。
遍历整个链表时,当当前node的val小于x时,接在小链表上,反之,接在大链表上。这样就保证了相对顺序没有改变,而仅仅对链表做了与x的比较判断。
最后,把小链表接在大链表上,别忘了把大链表的结尾赋成null。
代码如下:
1 public ListNode partition(ListNode head, int x) {
2 if(head==null||head.next==null)
3 return head;
4
5 ListNode small = new ListNode(-1);
6 ListNode newsmallhead = small;
7 ListNode big = new ListNode(-1);
8 ListNode newbighead = big;
9
while(head!=null){
if(head.val<x){
small.next = head;
small = small.next;
}else{
big.next = head;
big = big.next;
}
head = head.next;
}
big.next = null;
small.next = newbighead.next;
return newsmallhead.next;
}
Partition List leetcode java的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode算法题-Array Partition I(Java实现)
这是悦乐书的第262次更新,第275篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第129题(顺位题号是561).给定一个2n个整数的数组,你的任务是将这些整数分组为n对 ...
- Partition List -- LeetCode
原题链接: http://oj.leetcode.com/problems/partition-list/ 这是一道链表操作的题目,要求把小于x的元素按顺序放到链表前面.我们仍然是使用链表最经常使用 ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- kafka topic消息分配partition规则(Java源码)
我们知道Kafka 的消息通过topic进行分类.topic可以被分为若干个partition来存储消息.消息以追加的方式写入partition,然后以先入先出的顺序读取. 下面是topic和part ...
- 561. Array Partition I - LeetCode
Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
随机推荐
- rabbitmq学习之路-rabbitmqctl
rabbitmqctl使用 学习rabbitmq,原理之后第一个要掌握的就是rabbitmqctl这个命令的用法了,rabbitmq的管理功能最全的就是rabbitmqctl命令了,当然还有HTTP ...
- PHP接入微信H5支付
开发前配置 进行代码接入前,需在微信后台填写授权回调域名,此域名必须经过ICP备案 开发主要流程 用户下单时选择微信支付 商户进行业务逻辑处理并调用微信统一下单接口,微信H5交易类型为:trade_t ...
- Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server.
报错信息:Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQL ...
- leetcode easy problem set
*勿以浮沙筑高台* 持续更新........ 题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4m ...
- [ 转载 ] js十大排序算法:冒泡排序
js十大排序算法:冒泡排序 http://www.cnblogs.com/beli/p/6297741.html
- 解决springboot项目中@Value注解参数值为null的问题
1.错误场景: springboot项目中在.properties文件(.yml)文件中配置了属性值,在Bean中使用@Value注解引入该属性,Bean的构造器中使用该属性进行初始化,此时有可能会出 ...
- Cipolla算法学习小记
转自:http://blog.csdn.net/doyouseeman/article/details/52033204 简介 Cipolla算法是解决二次剩余强有力的工具,一个脑洞大开的算法. 认真 ...
- 【推导】The 16th UESTC Programming Contest Preliminary L - Foxtrot
题意:有n瓶药剂,其中只有一瓶药剂有毒.让你用最少的小白鼠试出哪瓶有毒.你只有一次给任意只小白鼠各喂食任意种类药剂的机会. m只老鼠就能对应2^m种“生死状态”的组合,给每种状态分配一个种类的药剂,然 ...
- hdu 4417 区间内比h小的数 线段树
题意求区间内比h小的数的个数 将所有的询问离线读入之后,按H从小到大排序.然后对于所有的结点也按从小到大排序,然后根据查询的H,将比H小的点加入到线段树,然后就是一个区间和. 2015-07-27:专 ...
- oracle监听理解 命名理解
一.监听器是oracle基于服务器端的一种网络服务,主要作用是监听客户端的连接请求,并将请求转发给服务器. 监听器基于端口的,每个监听器会占用一个端口.默认监听端口1521. oracle家目录下的n ...