博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode — partition-list
阅读量:7052 次
发布时间:2019-06-28

本文共 2697 字,大约阅读时间需要 8 分钟。

/** * 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
{ 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)); }}

转载于:https://www.cnblogs.com/sunshine-2015/p/7758445.html

你可能感兴趣的文章
聊聊hikari连接池的idleTimeout及minimumIdle属性
查看>>
设计模式 | 迭代器模式及典型应用
查看>>
1小时学会:最简单的iOS直播推流(十一)sps&pps和AudioSpecificConfig介绍(完结)...
查看>>
Oracle APEX 系列文章6:Oracle APEX 到底适不适合企业环境?
查看>>
ubuntu搭建nodejs生产环境——快速部署手册
查看>>
探索解析微服务下的RabbitMQ
查看>>
谈一谈 Spring-Mybatis 在多数据源配置上的坑
查看>>
SpringMVC源码解析系列4-HandleAdapter
查看>>
iOS开发中多线程的那些事
查看>>
使用 React 一年后,我学到的最重要经验
查看>>
字面量-数组、字典
查看>>
从零开始学Python(七):文件存储I/O流和异常捕捉
查看>>
JavaScript基础(5) - IDE与调试
查看>>
Android 性能优化之旅5 电量优化
查看>>
如何为你的App配置多环境变量
查看>>
学习OpenGL ES之什么是Shader?
查看>>
RxJava学习之结合(组合)型操作符
查看>>
Python基础(三): 数值和布尔
查看>>
从零开始实现一个简易的Java MVC框架
查看>>
iOS 12, watchOS 5, macOS Mojave 10 14, tvOS 12 等beta版描述文件下载
查看>>