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的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
随机推荐
- Hibernate学习总结
首先声明这是个坑爹的框架 属于ssh经典框架中的持久层框架,说白了就是管理数据库的. 下载地址:http://hibernate.org/orm/ 这里写了版本5.2,下载下来的基本不怎么会用,因为文 ...
- Java自定义异常类
用户可以根据自己的需要定义自己的异常类,定义异常类只需要继承Exception类即可 //================================================= // Fi ...
- 商城常用css分类代码
如图: 原代码如下: <div class="allMerchan bgnone"> <h2 class="ttlm_category"> ...
- border设置不占用宽度
经常我们设置好了DIV或其他标签的宽度,但是一加边框,宽度就又增加了,尤其是用百分比的时候,宽度控制不好真是麻烦! 如下有一解决办法,代码如下,(新属性,兼容性不好,手机端.谷歌.火狐测试可以) -w ...
- Django笔记-数据库操作(多对多关系)
1.项目结构 2.关键代码: data6.settings.py INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', ' ...
- Python基础之【第一篇】
Python简介: python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语 ...
- Fiddler源代码分享
frmViewer.cs: namespace Fiddler{ using Microsoft.Win32; using System; using System.Collecti ...
- Python之路【第十二篇】前端之js&dome&jQuery
JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚本是通过嵌入在HTML中来实现 ...
- from live writer
<body> <div class="pagination"> <a href="#" class="first&quo ...
- dom.style.left 与 dom.offsetLeft区别
dom.style.left 初始空值,必须在html行内样式定义值才有值,在css样式定义仍为空值 可读写,是字符串,读写是必须加px,否则无效 ...