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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. [LeetCode]86. Partition List分离链表

    /* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...

  7. LeetCode 86. 分隔链表(Partition List)

    86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...

  8. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  9. leetcode 143. Reorder List 、86. Partition List

    143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...

随机推荐

  1. uC/OS-II中includes块

    /*************************************************************************************************** ...

  2. js002-在HTML中使用JavaScript

    js002-在HTML中使用JavaScript 2.1            <script>元素   定义了以下6个属性   async: 可选.表示应该立即下载脚本,但不妨碍页面中的 ...

  3. WinForm------弹出MessageBox窗口的同时隐藏当前窗口

    private void Btn_OK_Click(object sender, EventArgs e) { this.Hide(); //隐藏当前窗口 MessageBox.Show(" ...

  4. MySQL------如何将SQLServer文件数据迁移到MySQL

    转载: http://blog.csdn.net/zhangdaiscott/article/details/46412453

  5. 蛋疼的vs

    这个vs2008 难用的很,要是叫我选肯定vs高版本的,vs2012或者直接vs2015

  6. windows查看占用端口的进程

    1方法 先找到进程号: netstat -aon|findstr 再根据进程号得到进程: tasklist |findstr " 2结果

  7. Exception Handling in ASP.NET Web API webapi异常处理

    原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...

  8. DNX 版本升级命令

    一.稳定版本 dnvm install latest -a x86 -r clrdnvm install latest -a x86 -r coreclrdnvm install latest -a ...

  9. android自定义控件(2)-拖拽实现开关切换

    在这里,我们的主要工作就是在原有代码的基础上,增加一个重写的onTouchEvent方法,刚添加上来的时候是这个样子的: @Override public boolean onTouchEvent(M ...

  10. data and dream

    1 用通俗的语言介绍下线性回归->逻辑回归->SVM之间的区别和联系. 2 聚类算法的应用场景,以及k-means中的k值怎么确定. def center(data): center = ...