/**
* Source : https://oj.leetcode.com/problems/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 class Partition { /**
* 将链表中所有小于x的节点排在前面,然后是大于等于x的节点,partition后的链表要按照之前元素的相对顺序排序
*
* 将所有大于等于x的节点移除到另外一个链表,剩下的就是小于x的元素,然后将两个链表连接起来
*
* 因为链表头也可能被移除(可能变化),所以这里使用一个dummy节点指向原来的头,作为新的头,也就是链表的头
*
* @param head
* @return
*/
public Node partition (Node head, int x) {
Node dummy = new Node();
dummy.next = head;
head = dummy;
Node greaterList = new Node();
Node greaterPointer = greaterList; while (head != null && head.next != null) {
if (head.next.value >= x) {
// 加入新链表
greaterPointer.next = head.next;
head.next = head.next.next;
greaterPointer = greaterPointer.next;
greaterPointer.next = null;
// 从原来的链表移除
} else {
head = head.next;
}
}
head.next = greaterList.next;
return dummy.next;
} private static class Node implements Comparable<Node>{
int value;
Node next; @Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + (next == null ? "" : next.value) +
'}';
} @Override
public int compareTo(Node o) {
return this.value - o.value;
}
} private static void print (Node node) {
while (node != null) {
System.out.println(node);
node = node.next;
}
System.out.println();
} public Node createList (int[] arr) {
if (arr.length == 0) {
return null;
}
Node head = new Node();
head.value = arr[0];
Node pointer = head;
for (int i = 1; i < arr.length; i++) {
Node node = new Node();
node.value = arr[i];
pointer.next = node;
pointer = pointer.next;
}
return head;
} public static void main(String[] args) {
Partition partition = new Partition();
int[] arr = new int[]{1,4,3,2,5,2};
int[] arr1 = new int[]{4,3,2,5,2};
print(partition.partition(partition.createList(arr1), 3));
print(partition.partition(partition.createList(arr), 3));
}
}

leetcode — partition-list的更多相关文章

  1. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  2. [LeetCode] 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] Partition Labels 分割标签

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  4. [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

  5. [leetcode]Partition List @ Python

    原题地址:https://oj.leetcode.com/problems/partition-list/ 题意: Given a linked list and a value x, partiti ...

  6. LeetCode Partition to K Equal Sum Subsets

    原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...

  7. Leetcode Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  8. LeetCode - Partition Labels

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  9. LeetCode: Partition List 解题报告

    Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...

  10. Leetcode: Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

随机推荐

  1. 大数据 - Java基础:读取键盘输入的方法

    Java中获取键盘输入值的三种方法 程序编写中,从键盘获取数据是一件非常普通又平常的事 C:scanf() C++:cin() C#:Read().ReadKey().ReadLine() Java没 ...

  2. mac效率工具

    前言:在命令行中切换目录是最常用的操作,我相信一遍又一遍重复“cd ls cd ls cd ls ……”绝对会让你抓狂. 记录一下,方便下次系统重装,哈哈 一. oh-my-zsh mac 预装了 z ...

  3. input框中修改placeholder的样式

    有时间input标签的placeholder属性会出现问题,下面是修改placeholder的样式demo input::-webkit-input-placeholder{ color:red; f ...

  4. Golang的模块管理Module

    Golang 1.11版本终于支持了官方的模块依赖管理功能,1.11以前想要实现依赖管理只能够通过借助第三方库来实现,1.11以前的版本Golang项目必须依赖以GOPATH,从当前版本开始Golan ...

  5. 树莓派0 ubuntu无显示器ssh登录终端

    在此记录倒腾树莓派的过程 一.本文前提 已经装好系统,我装的是官方的Raspbian系统,以下是系统下载地址和工具地址 (默认帐号:pi,默认密码:raspberry) 镜像下载: http://do ...

  6. zuoye

    a=input('请输入一个数字:') b=input('请再输入一个数字') sum2=int(a)+int(b) print('两个数字的和是:{}'.format(sum2)) a=input( ...

  7. matlab安装 macos

    http://pan.baidu.com/s/1o6qKdxo内附安装说明Matlab R2014A Mac & Linux 破解版 readme文件有流程!可以安装

  8. session源码剖析

    session机制采用的是一种在客户端与服务端之间保持状态的解决方案,由于采用服务器端保持状态的方案在客户端也要保存标识,session机制也要借助于cookie机制达到目的.session保存了客户 ...

  9. 了解vue APi

    阳光那么好,何必自寻烦恼,过好每一个当下,一万个美丽的未来抵不过一个温暖的现在. 一.Vue.nextTick(): 该api 是在Dom节点更新结束之后执行的一个延时回调.在修改数据之后,立即使用这 ...

  10. 编写程序,输入一个N,返回角谷变换(达到1所需)的次数

    import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/6 17:36 * @description ...