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 ...
随机推荐
- Git 使用vi或vim命令打开、关闭、保存文件
1.vi & vim 有两种工作模式: (1) 命令模式:接受.执行 vi & vim 操作命令的模式,打开文件后的默认模式: (2) 编辑模式:对打开的文件内容进行 增.删.改 操作 ...
- Android实现按两次back键退出应用
重写onKeyDown()方法 System.currentTimeMillis():该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0 ...
- OVN实战---《OVN and Containers》翻译
Overview 在本篇文章中,我们要讨论的是OVN和容器的集成.到本次实验中,我们将会创建一个包含有一对容器的“虚拟机”,这些容器会直接和OVN logical switch相连,并且可以供逻辑网络 ...
- CMDB实现的四种方式
第一种(agent): 这种方式是通过向每一台服务器安装agent脚本,然后通过中控机的API,来收集所需要的数据,最后放到数据库中,在通过web的方式显示出来. 实现流程图: 1.录入资产(主机名, ...
- 【云安全与同态加密_调研分析(4)】云计算安全领域主要研究成果——By Me
下表列举了在云安全问题研究表现突出的ICT公司和研究机构以及其在云计算安全方面主要研究成果: ◆ICT公司和研究机构(云计算安全领域主要研究成果)◆ ◆机构名称◆ ◆机构类别◆ ◆主要研究成果◆ ◆备 ...
- java7(3)——增强的catch之自动释放资源
跟mutilcatch一样,java7提供了自动释放资源的方法,但还是很少看到人使用,估计是麻烦去重写close方法.不过jdk内部一些类已经改成使用增强的catch来释放资源的写法,所以我们有必要了 ...
- python web框架 Django 登录页面
在django 项目下 创建一个templates 放模板的文件夹 html文件都放在这里 在里面写一个login.html 登录页面 urls.py 加上 login 对应关系 from djang ...
- java-mybaits-00301-SqlMapConfig
1.配置内容 mybatis的全局配置文件SqlMapConfig.xml,SqlMapConfig.xml中配置的内容和顺序如下: properties(属性) settings(全局配置参数) t ...
- odoo学习记录1
1. odoo通过ORM(对象关系映射)实现底层数据与上层逻辑到关联,保证数据存储的安全性和使用上到便利性. 2. odoo由模块组成,每个模块包含:Bussiness Object, Data, W ...
- Spring框架第二篇之Bean的装配
一.默认装配方式 代码通过getBean();方式从容器中获取指定的Bean实例,容器首先会调用Bean类的无参构造器,创建空值的实例对象. 举例: 首先我在applicationContext.xm ...