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.
For example,
Given 1->4->3->2->5->2
and x = 3,
return 1->2->2->4->3->5
.
题意:
给出一个链表和一个数字x,将链表分区,小于x的都放在大于等于x的前面,而且要保持每个分区内各结点的相对位置没有发生变化。
比如原链表中,4在3的前面,3在5的前面,分区后依然要保持这个相对位置。
过程:
(1)新建两个链表,一个first,一个second,
一个用来记录链表中所有小于x的结点,一个用于记录链表中所有大于等于x的结点。
(2)遍历原链表。
(3)最后将两个链表连接起来作为结果返回。
public class Solution {
public ListNode partition(ListNode head, int x) {
if(head == null) return null; ListNode firstDummy = new ListNode(0);
ListNode secondDummy = new ListNode(0);
ListNode first = firstDummy, second = secondDummy; while(head != null){
if(head.val < x){
first.next = head;
first = head;
}else{
second.next = head;
second = head;
}
head = head.next;
} first.next = secondDummy.next;
second.next = null;
return firstDummy.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 解题思路
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 ...
- 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分离链表
/* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...
- LeetCode 86. 分隔链表(Partition List)
86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
随机推荐
- document.execCommand 常用的方法
execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用如下格式: document.execCommand(sCommand[,交互方式, 动态参数]) , ...
- Hook技术
hook钩子: 使用技术手段在运行时动态的将额外代码依附现进程,从而实现替换现有处理逻辑或插入额外功能的目的. 它的技术实现要点有两个: 1)如何注入代码(如何将额外代码依附于现有代码中). 2)如何 ...
- 【原】http缓存与cdn相关技术
摘要:最近要做这个主题的组内分享,所以准备了一个星期,查了比较多的资料.准备的过程虽然很烦很耗时间,不过因为需要查很多的资料,因此整个过程下来,对这方面的知识影响更加深刻.来来来,接下来总结总结 一 ...
- angularjs中$watch监听model(对象属性、对象)变化
昨天看了一下教学视频,学到了有关$watch的用法,想到最近做的一个页面中有个select下拉选项(select中的值变化了,则后面input中的值也跟着相应的变化),不知是否可以使用$watch来代 ...
- WinForm------TreeList加载数据方法
1.SQLService操作 (1)在SQLServer创建一张表dbo.Department (2)写入以下数据 2.VS操作(这里如何使用EntityFramework加载数据库就不多说了哈) ( ...
- socketserver模块写的一个简单ftp程序
一坨需求... 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp server上随意切换目录 (cd) ...
- c#根据公式进行自动计算 四个5加减乘除=4
想不出来就采用枚举法吧,代码写起来还是很简单的,当然代码写的不怎么样,也不考虑设计效率等等问题了,理论上这种类型的都可以这么拼出来,比较初级的做法,但轻松解决问题.注意Calculate(st ...
- JavaWeb学习笔记——javabean
- SQL 使用小记
1. case语句 示例 select id, name, case user_role then "管理员" then "未注册用户" then " ...
- Google 谷歌网页搜索, 学术搜索
Google 谷歌网页搜索, 学术搜索 1. 网页搜索引擎-Google * https://letsgg.tk/ * https://google.kfd.me/ 谷歌搜索镜像: http://d ...