[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 ...
随机推荐
- 全志A33 lichee lvds屏幕配置
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 芯灵思SinlinxA33开 ...
- Java_IO_文件的续写_小笔记
package IO; import java.io.FileWriter; import java.io.IOException; class FileWrite_WenJianXuXie { /* ...
- zabbix之 zabbix模板监控mysql
zabbix中默认有mysql的监控模板.默认已经在zabbix2.2及以上的版本中.模板名称:Template App MySQL.如果没有则要去zabbix官方下载 url:https://zab ...
- 第六届蓝桥杯省赛 java三羊献瑞
将文字看作一个个变量.根据一开始确定的文字的值进行暴力循环. 三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉 + 三 羊 献 瑞------------------- 三 羊 生 瑞 气 (如果有对齐 ...
- globals和locals的区别
Python的两个内置函数,locals 和globals,它们提供了基于字典的访问局部和全局变量的方式. 1.locals()是只读的.globals()不是.这里说的只读,是值对于原有变量的只读. ...
- __slots__用法
class Test(object): __slots__ = ("name","age") t = Test() t.name = "老王" ...
- (转载)o(1), o(n), o(logn), o(nlogn) 时间复杂度
o(1), o(n), o(logn), o(nlogn) 时间复杂度的解释: https://blog.csdn.net/yhc166188/article/details/81162865 时间复 ...
- 石板地面 Base Shape
软件:Substance Designer 2017.1.2 石板地面 Base Shape 效果见图一 图一:Base Shape (2D View) 首先使用Cells 1(Pattern)结点生 ...
- springmvc的简单使用以及ssm框架的整合
Spring web mvc是基于servlet的一个表现层框架 首先创建一个简单的web工程了解它的使用 web.xml的配置 <?xml version="1.0" en ...
- Java八大排序算法
Java八大排序算法: package sort; import java.util.ArrayList; import java.util.Arrays; import java.util.List ...