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));
}
}
leetcode — partition-list的更多相关文章
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [LeetCode] Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- [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 ...
- [leetcode]Partition List @ Python
原题地址:https://oj.leetcode.com/problems/partition-list/ 题意: Given a linked list and a value x, partiti ...
- LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
- Leetcode Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- LeetCode - Partition Labels
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- LeetCode: Partition List 解题报告
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- Leetcode: Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
随机推荐
- 在github上面创建新的分支
第一步:git branch 查看当前分支情况 git branch //查看当前分支情况 第二步:git branch 分支名,新建一个自己的分支 git branch 分支名 // 新建一个自己的 ...
- Chapter 5 : Control Structures 2 : Repetition
import java.util.*; import java.io.*; public class Loop { static Scanner console = new Scanner(Syste ...
- C++ 用变量定义数组
较早的编译器是不同意这样做的,所以一些书籍比方以Tc解说的书本都说数组的下标不能是变量.在vc6.0下亦是如此. 只是在一些较新的编译器如dev c++已经支持了,例如以下代码不会报错 #includ ...
- 微信跳转ticket值怎么得到?浏览器跳到微信?哪里有微信跳转接口?跳转功能能用多久?
目前很多实用微信跳转技术的电商朋友,网站文章头部或者文章中部出现了点击关注微信关注的二维码,用户点击进去直接跳转到微信内打开指定的二维码,识别即可关注,方便省事,比以往的一键复制—粘贴微信号,转化效果 ...
- 虚拟机下Ubuntu扩容及磁盘重新分区-Gparted
转自: https://blog.csdn.net/timsley/article/details/50742755
- javaWeb+servlet+mysql实现简单的企业员工管理系统
企业员工信息管理系统 一.源码描述 本程序为企业员工信息管理系统.是javaEE一个系统,主要实现登录功能和两个模块信息的增删改查.可以作为JAVAweb学习,也可在原有基础上进行深一步的 ...
- HTML块元素,行内元素,类,头部元素
总结HTML块元素,行内元素,类,头部元素 块元素: 在HTML中,块级元素的高度为其内容的高度,宽度会扩展到与父容器同宽.默认情况下,块级元素会独占一行,并且元素前后行留空. 示例:<h1&g ...
- python编程从入门到实战1-3章
print('hellow world') """ 多行注释"""#大小写print('i love you')mssage='hellow ...
- java小练习
打印99乘法表 因为有9行9列,所有要用两个for循环 int m; for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) ...
- .Net深入实战系列—JSON序列化那点事儿
序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer ...