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,
Given 1->4->3->2->5->2
and x = 3,
return 1->2->2->4->3->5
.
算法分析:开始我只是想在原来链表上进行操作,但是无法返回头结点。这道题区别链表反转,链表反转不用新建链表,只用在原有的链表上操作就行了。这道题,要新建两个新的链表,一个链表的元素全部小于目标值,另一个链表的元素全部大于目标值。然后把这两个链表连接起来。
public ListNode partition(ListNode head, int x)
{
if(head == null || head.next == null)
{
return head;
}
ListNode lessHead = new ListNode(0);
ListNode greaterHead = new ListNode(0);
ListNode less = lessHead, greater = greaterHead;
ListNode node = head;
while(node != null)
{
ListNode temp = node.next;
if(node.val < x)
{
less.next = node;
less = less.next;
less.next = null;
}
else
{
greater.next = node;
greater = greater.next;
greater.next = null;
}
node = temp;
}
less.next = greaterHead.next;
return lessHead.next;
}
Partition List,拆分链表的更多相关文章
- [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 ...
- 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划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 086 Partition List 分隔链表
给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前.你应当保留两个分区中每个节点的初始相对位置.例如,给定1->4->3->2-&g ...
- 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 ...
- Leetcode86. Partition List分隔链表(双指针)
给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4-&g ...
随机推荐
- 日期格式私人定制——SimpleDateFormat
[前言] 最近项目需要特殊的日期格式,又恰好是String类型的,以前都没怎么用到SimpleDateFormat这个类去格式化日期,脑子里蹦出来的思路就是先把Date给toString了,然后慢慢切 ...
- python和shell之间变量的相互调用
python -> shell: 1.环境变量 2.字符串连接 3.通过管道 import os var=’123’ os.popen(’wc -c’, ’w’).write(var) 4.通过 ...
- Wilcoxon符号秩+秩和检验学习[转载]
参数检验就是已知数据的精确分布模型,根据数据来求出模型中的未知参数:而非参数检验就是无需对样本总体分布(比如满足正态分布)做出假设. 1.符号检验 转自:https://baike.baidu.com ...
- 从iOS的图片圆角想到渲染
圆角是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受.设置圆角会带来一定的性能损耗,如何提高性能是一个需要重点讨论的话题. 大家常见的圆角代码x.layer.cornerRadius = ...
- 处理函数和数组声明[条款17]---《C++必知必会》
指向函数的指针声明和指向数组的指针声明容易混淆,原因在于函数和数组修饰符的优先级比指针修饰符的优先级高,因此通常需要使用圆括号. int *f1( );//一个返回值为 int* 的函数 int ( ...
- .net操作xml文件(新增.修改,删除,读取) 转
今天有个需求需要操作xml节点.突然见遗忘了许多.上网看了些资料.才整出来.脑袋真不够用.在这里把我找到的资料共享一下.方便以后使用.本文属于网摘/ 1 一.简单介绍2 using System.Xm ...
- Oblect类之hashCode和equals
1.hashCode的默认实现.显然是一个本地方法. 2.equals的默认实现.默认equals在比较两个对象时,是看他们是否指向同一个地址的.“==”操作比较的是两个变量的值是否相等,对于引用型变 ...
- PKU 1201 Intervals(差分约束系统+Spfa)
题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...
- JQuery如何实现双击事件时不触发单击事件,解决鼠标单双击冲突问题
在jQuery的事件绑定中,如果元素同时绑定了单击事件(click)和双击事件(dblclick),那么执行单击事件(click)时,不会触发双击事件(dblclick), 执行双击事件(dblcli ...
- 【android】开发笔记系列UI篇
弹出View添加阴影效果 系统自带就有,在android studio上直接写入背景颜色 android:background="@android:drawable/dialog_holo_ ...