partition-list
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,
Given1->4->3->2->5->2and x = 3,
return1->2->2->4->3->5.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode partition(ListNode head, int x) {
if(head==null)
return null;
ListNode smallHead=new ListNode(0);
ListNode bigHead=new ListNode(0);
ListNode small=smallHead;
ListNode big=bigHead;
ListNode p=head;
while(p!=null){
if(p.val>=x){
big.next=p;
big=big.next;
}else{
small.next=p;
small=small.next;
}
p=p.next;
}
small.next=bigHead.next;
big.next=null;//必须把后面断掉,因为原来的节点还指着后面。我这里犯错误了,没有断掉,导致报错
return smallHead.next;
}
}
最新文章
- LeetCode-1TwoSum(C#)
- shell 脚本技巧
- C#中==与Equals方法的区别
- 第7章 jQuery插件的使用和写法
- DOM解析
- Java-Stack
- Codeforces Round #293 (Div. 2)
- Appium 切换上下文环境
- [转]PO和VO、关于延迟加载(lazy)和强制加载(Hibernate.initialize(Object proxy) )
- 从一次面试经历谈PHP的普通传值与引用传值以及unset
- MySQL优化GROUP BY-松散索引扫描与紧凑索引扫描
- cookie特殊字符在游览器被转义
- .net core 2.0 数据访问-迁移
- h5调用手机相册摄像头以及文件夹
- Ubuntu 守护进程
- 实用的php购物车程序
- PHP注释的艺术——phpDoc规范
- 2018-2019 ACM-ICPC 焦作赛区 部分题解
- Oracle之标示符无效
- Docker 三剑客
热门文章
- HDU 3078 (LCA+树链第K大)
- C++11 feature: move constructor
- Code[VS] 2370 LCA 题解
- 定义 iOS 方法名等不错的规范
- SpringMVC+Thymeleaf如何处理URL中的动态查询参数
- window下 配置gitlab ssh非端口22端口
- HighchartsJS创建点状带标识的图表实例
- EasyUI组件(窗口组件)
- while:1.兔子生兔子问题 2.打印菱形 3.求100以内质数的和4.洗发水15元一瓶,牙膏5元一支,香皂2元一块,150元刚好花完
- Winform程序以Icon的形式显示在任务栏右下角