leetcode — partition-list

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值