[leetcode]86. 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.
Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5
题意:
给定一个链表和一个值,把小于等于和大于该值的部分分别放到链表的一前一后去。
思路:
先分为两个链表
然后合并
代码:
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode leftDummy = new ListNode(-1);
ListNode rightDummy = new ListNode (-1);
ListNode left_cur = leftDummy;
ListNode right_cur = rightDummy;
ListNode cur = head; while( cur != null){
if(cur.val < x){
left_cur.next = cur;
left_cur = cur;
}else{
right_cur.next = cur;
right_cur = cur;
}
cur = cur.next;
}
left_cur.next = rightDummy.next;
right_cur.next = null;
return leftDummy.next;
}
[leetcode]86. Partition List划分链表的更多相关文章
- [LeetCode] 86. Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- LeetCode 86. Partition List 划分链表 C++
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [LeetCode]86. Partition List分离链表
/* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...
- leetCode 86.Partition List(分区链表) 解题思路和方法
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [CareerCup] 2.4 Partition List 划分链表
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- leetcode 86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- partition List(划分链表)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [LeetCode] 86. Partition List 解题思路
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
随机推荐
- PythonStudy——Python 内存池机制 (Memory pool mechanism) Pymalloc
Python是如何进行内存管理-内存池机制 Pymalloc Python引用了一个内存池(memory pool)机制,即Pymalloc机制(malloc:n.分配内存),用于对小块内存的申请和释 ...
- redis.conf 配置信息:读取及修改命令
相关资源 网址 官方地址(网页中 Command + F,输入井号"#",方便查看没有注释的行) http://download.redis.io/redis-stable/red ...
- webpack 中,importloaders 配置项的含义
importLoaders:用于配置「css-loader 作用于 @import 的资源之前」有多少个 loader. 0 => no loaders (default); 1 => p ...
- 如何在idea中引入一个新maven项目
如何在idea中引入一个新的maven项目,请参见如下操作:
- dependency walker检查dll依赖关系目录设置的问题
废话少说,直接上图 图中来看,似乎IESHIMS.DLL文件不存在报错,实际是因为没有加载IESHIMS.DLL所在的路径. 在我的电脑里面搜索有两个同名的dll,一个是32位的,一个是64位的. C ...
- Python发送微信消息
针对此 需要安装itchat第三方模块 采用pip安装就可以了 pip install itchat import itchatimport time#引入时间函数进行测试time_format= ...
- servlet cdi analysis
CDI中最令人兴奋的功能是允许每个人在Java EE平台中编写强大的扩展性功能,甚至于改变其核心本身.这些扩展性功能是可以完全移植到任何支持CDI的环境中. CDI的一些主要特性 1.类型安全:CDI ...
- html/css/js-如何利用jq来更改属性的值和获取属性的值
jquery的使用在web开发中是非常广泛的,虽然说比较容易,易学,但在开发过程中,也总是会碰到各种各样的小问题. 我曾经就遇到这种问题,jq如何获取属性值和更改属性值的. 众所周知,attr()可以 ...
- 非常优秀的swiper插件————幻灯片播放、图片轮播
http://www.idangero.us/ http://www.swiper.com.cn/ Swiper中文网 2015-10-15 SuperSlide2: (这是个PC用的滚屏插件,看着不 ...
- python函数-基础篇
函数 为什么要用函数?1.减少代码冗余2.增加代码可读性 函数的定义及使用 def info(): # 这里我们定义一个打印个人信息的函数 name = "xiaoming" ag ...