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 ...
随机推荐
- tensorflow 使用 5 mnist 数据集, softmax 函数
用于分类 softmax 函数 手写数据识别:
- c#提交事务的两种方法
1. using (TransactionScope ts = new TransactionScope()) { 除非显示调用ts.Complete()方法.否则,系统不会自动提交这个事务.如果在代 ...
- 用idea 创建一个spring小demo,基于xml文件配置
1.首先,File->new->project ,进入新增项目页面 或者在 2.勾选spring,然后点击下一步 3.修改项目名称和项目位置 进入页面后 5.创建一个spring配置文件 ...
- Java_接口与抽象类
接口: 接口,英文interface,在java中,泛指供别人调用的方法或函数.接口是对行为的一种抽象. 语法: [public] interface InterfaceName{} 注意: 1)接口 ...
- Hadoop集群搭建过程中ssh免密码登录(二)
一.为什么设置ssh免密码登录 在集群中,Hadoop控制脚本依赖SSH来执行针对整个集群的操作.例如,某个脚本能够终止并重启集群中的所有守护进程.所以,需要安装SSH,但是,SSH远程登陆的时候,需 ...
- Android Studio 设置不同分辨率的图标Icon
右键你的项目 -->"NEW"-->"Image Asset" 'Asset Type' 勾选”Image“才可以选择”Path“,其他选项可以自己 ...
- JavaWeb学习路线
一.三大组件介绍 javaweb在开发中有三大组件分别提供不同的功能,这三大组件为servlet,filter,listener 1.servlet 简单来说就是客户端请求服务器和接受服务器的响应,狭 ...
- 神经网络_线性神经网络 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)是神经元 ...
- js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法:
js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法: var s = new MyString('hello'); s.length; s[0]; // " ...
- [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 ...