/**
* 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. entOS7查看开放端口命令

    CentOS7的开放关闭查看端口都是用防火墙来控制的,具体命令如下: 查看已经开放的端口: firewall-cmd --list-ports 开启端口 firewall-cmd --zone=/tc ...

  2. Web框架之Django篇

    1.创建Project 命令: django-admin startproject mysite 2.配置 (1)模板配置 (2)静态文件配置 (3)csrf隐藏 3.路由关系 一个url对应一个函数 ...

  3. 如何查看ubuntu系统版本信息

    第一种方法: hadoop@master:~$ cat /proc/version Linux version 4.4.0-21-generic (buildd@lgw01-21):Linux内核版本 ...

  4. node07

    ---恢复内容开始--- 1.SQL基本查询语句 2.子句 1)WHERE 子句 WHERE key=val WHERE key>val WHERE key1>val1 AND key2& ...

  5. 记一次JVM故障排除

    今天,自己开发的事件驱动的java大规模爬虫程序上线了几个新任务后突然异常. 异常: 程序业务异常,经查看CPU利用率满,内存满,一直报OOM,目测有内存泄露.如下图所示,四核16G的内粗,CPU高达 ...

  6. 使用ANY和ALL条件

    在比较运算符中,可以出现ALL和ANY,表示“全部”和“任一”,但是ALL和ANY不能单独使用,需要配合单行比较操作符>.>=.<.<=一起使用.其中: > ANY : ...

  7. python穷举法解数独

    总体思路 : 数独九行九列,一个list装一行,也就需要一个嵌套两层的list 初始会有很多数字,我可不想一个一个赋值 那就要想办法偷懒啦 然后再是穷举,如何科学的穷举 第一部分:录入 某在线数独网站 ...

  8. 多选ui实现单选效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Linux下CenOS系统 安装Mysql-5.7.19

    1.输入网址https://www.mysql.com/downloads/,进入downloads,选择Community 2.选择对应的版本和系统: 输入命令:wget https://cdn.m ...

  10. External Snapshot management

    External Snapshot management Symptom As of at least libvirt 1.1.1, external snapshot support is inco ...