1. /**
  2. * Source : https://oj.leetcode.com/problems/partition-list/
  3. *
  4. *
  5. * Given a linked list and a value x, partition it such that all nodes less than x come
  6. * before nodes greater than or equal to x.
  7. *
  8. * You should preserve the original relative order of the nodes in each of the two partitions.
  9. *
  10. * For example,
  11. * Given 1->4->3->2->5->2 and x = 3,
  12. * return 1->2->2->4->3->5.
  13. */
  14. public class Partition {
  15. /**
  16. * 将链表中所有小于x的节点排在前面,然后是大于等于x的节点,partition后的链表要按照之前元素的相对顺序排序
  17. *
  18. * 将所有大于等于x的节点移除到另外一个链表,剩下的就是小于x的元素,然后将两个链表连接起来
  19. *
  20. * 因为链表头也可能被移除(可能变化),所以这里使用一个dummy节点指向原来的头,作为新的头,也就是链表的头
  21. *
  22. * @param head
  23. * @return
  24. */
  25. public Node partition (Node head, int x) {
  26. Node dummy = new Node();
  27. dummy.next = head;
  28. head = dummy;
  29. Node greaterList = new Node();
  30. Node greaterPointer = greaterList;
  31. while (head != null && head.next != null) {
  32. if (head.next.value >= x) {
  33. // 加入新链表
  34. greaterPointer.next = head.next;
  35. head.next = head.next.next;
  36. greaterPointer = greaterPointer.next;
  37. greaterPointer.next = null;
  38. // 从原来的链表移除
  39. } else {
  40. head = head.next;
  41. }
  42. }
  43. head.next = greaterList.next;
  44. return dummy.next;
  45. }
  46. private static class Node implements Comparable<Node>{
  47. int value;
  48. Node next;
  49. @Override
  50. public String toString() {
  51. return "Node{" +
  52. "value=" + value +
  53. ", next=" + (next == null ? "" : next.value) +
  54. '}';
  55. }
  56. @Override
  57. public int compareTo(Node o) {
  58. return this.value - o.value;
  59. }
  60. }
  61. private static void print (Node node) {
  62. while (node != null) {
  63. System.out.println(node);
  64. node = node.next;
  65. }
  66. System.out.println();
  67. }
  68. public Node createList (int[] arr) {
  69. if (arr.length == 0) {
  70. return null;
  71. }
  72. Node head = new Node();
  73. head.value = arr[0];
  74. Node pointer = head;
  75. for (int i = 1; i < arr.length; i++) {
  76. Node node = new Node();
  77. node.value = arr[i];
  78. pointer.next = node;
  79. pointer = pointer.next;
  80. }
  81. return head;
  82. }
  83. public static void main(String[] args) {
  84. Partition partition = new Partition();
  85. int[] arr = new int[]{1,4,3,2,5,2};
  86. int[] arr1 = new int[]{4,3,2,5,2};
  87. print(partition.partition(partition.createList(arr1), 3));
  88. print(partition.partition(partition.createList(arr), 3));
  89. }
  90. }

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. tensorflow 使用 5 mnist 数据集, softmax 函数

    用于分类  softmax 函数 手写数据识别:

  2. c#提交事务的两种方法

    1. using (TransactionScope ts = new TransactionScope()) { 除非显示调用ts.Complete()方法.否则,系统不会自动提交这个事务.如果在代 ...

  3. 用idea 创建一个spring小demo,基于xml文件配置

    1.首先,File->new->project ,进入新增项目页面 或者在 2.勾选spring,然后点击下一步 3.修改项目名称和项目位置 进入页面后 5.创建一个spring配置文件 ...

  4. Java_接口与抽象类

    接口: 接口,英文interface,在java中,泛指供别人调用的方法或函数.接口是对行为的一种抽象. 语法: [public] interface InterfaceName{} 注意: 1)接口 ...

  5. Hadoop集群搭建过程中ssh免密码登录(二)

    一.为什么设置ssh免密码登录 在集群中,Hadoop控制脚本依赖SSH来执行针对整个集群的操作.例如,某个脚本能够终止并重启集群中的所有守护进程.所以,需要安装SSH,但是,SSH远程登陆的时候,需 ...

  6. Android Studio 设置不同分辨率的图标Icon

    右键你的项目 -->"NEW"-->"Image Asset" 'Asset Type' 勾选”Image“才可以选择”Path“,其他选项可以自己 ...

  7. JavaWeb学习路线

    一.三大组件介绍 javaweb在开发中有三大组件分别提供不同的功能,这三大组件为servlet,filter,listener 1.servlet 简单来说就是客户端请求服务器和接受服务器的响应,狭 ...

  8. 神经网络_线性神经网络 2 (Nerual Network_Linear Nerual Network 2)

    1 LMS 学习规则 1.1 LMS学习规则定义 MSE=(1/Q)*Σe2k=(1/Q)*Σ(tk-ak)2,k=1,2,...,Q 式中:Q是训练样本:t(k)是神经元的期望输出:a(k)是神经元 ...

  9. js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法:

    js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法: var s = new MyString('hello'); s.length; s[0]; // " ...

  10. [Swift]LeetCode815. 公交路线 | Bus Routes

    We have a list of bus routes. Each routes[i]is a bus route that the i-th bus repeats forever. For ex ...