leetcode — reorder-list
/**
* Source : https://oj.leetcode.com/problems/reorder-list/
*
* Given a singly linked list L: L0→L1→…→Ln-1→Ln,
* reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
*
* You must do this in-place without altering the nodes' values.
*
* For example,
* Given {1,2,3,4}, reorder it to {1,4,2,3}.
*/
public class RecordList {
/**
* 将链表的后半部分翻转之后依次插入链表前半部分每个元素后面
*
* 设链表长度为n
* 1. 找到链表后半部分起始位置: n/2+1,使用双指针法,slow每次移动一个,fast每次移动两个,fast移动到最后的时候,slow指向的正好是 n/2+1
* 2. 反转后半部分连链表
* 3. 将反转后的后半部分链表依次插入前半部分,left指向左边,right指向反转后的第一个node,依次插入left的后一个,直到right指向null,
* right每次移动一个node,left每次移动2个node(因为刚刚left后面插入一个节点)
*
* @param head
* @return
*/
public LinkedNode record (LinkedNode head) {
if (head == null || head.next == null) {
return head;
}
LinkedNode midNode = findMidNode(head);
LinkedNode reversedList = reverse(midNode);
LinkedNode left = head;
LinkedNode right = reversedList;
while (right != null && right.next != null) {
// 记录将要被插入的元素
LinkedNode target = right;
// 下一个需要被插入的元素
right = right.next;
// 插入target到链表中
target.next = left.next;
left.next = target;
left = left.next.next;
}
return head;
}
private LinkedNode findMidNode (LinkedNode head) {
LinkedNode slow = head;
LinkedNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
private LinkedNode reverse (LinkedNode head) {
LinkedNode p = head;
LinkedNode pre = null;
while (p != null) {
LinkedNode next = p.next;
p.next = pre;
pre = p;
p = next;
}
return pre;
}
private class LinkedNode {
int value;
LinkedNode next;
}
/**
* 创建普通的链表
* @param arr
* @return
*/
public LinkedNode createList (int[] arr) {
if (arr.length == 0) {
return null;
}
LinkedNode head = new LinkedNode();
head.value = arr[0];
LinkedNode pointer = head;
for (int i = 1; i < arr.length; i++) {
LinkedNode node = new LinkedNode();
node.value = arr[i];
pointer.next = node;
pointer = pointer.next;
}
return head;
}
private static void print (LinkedNode head) {
if (head == null) {
System.out.println("[]");
}
StringBuffer stringBuffer = new StringBuffer("[");
while (head != null) {
stringBuffer.append(head.value);
stringBuffer.append(",");
head = head.next;
}
stringBuffer.deleteCharAt(stringBuffer.length()-1);
stringBuffer.append("]");
System.out.println(stringBuffer);
}
public static void main(String[] args) {
RecordList recordList = new RecordList();
int[] arr = new int[]{1,2,3,4,5,6};
print(recordList.record(recordList.createList(arr)));
}
}
leetcode — reorder-list的更多相关文章
- [LeetCode] Reorder List 链表重排序
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...
- [leetcode]Reorder List @ Python
原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...
- Leetcode: Reorder List && Summary: Reverse a LinkedList
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...
- [Leetcode] Reorder list 重排链表
Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...
- [LeetCode] Reorder List 反向插入链表
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- LeetCode Reorder List
struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- 【Leetcode】Reorder List JAVA
一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must ...
随机推荐
- Swagger服务API治理详解
swager2的简介 在App后端开发中经常需要对移动客户端(Android.iOS)提供RESTful API接口,在后期版本快速迭代的过程中,修改接口实现的时候都必须同步修改接口文档,而文档与代码 ...
- SAML2.0 协议初识(一)
一.什么是 SAML 协议? SAML 即安全断言标记语言,英文全称是 Security Assertion Markup Language.它是一个基于 XML 的标准,用于在不同的安全域(secu ...
- Android Static分析
1.static的变量或者方法会放在内存的一个方法区内 2.static变量会在该变量所属的类从内存中释放掉之后. 才会释放掉而类释放的条件是非常苛刻的. 必需要该类相应的全部对象被释放掉. 同一时候 ...
- PKI(公钥基础设施)基础知识笔记
数字签名 数字签名(又称公钥数字签名.电子签章)是一种类似写在纸上的普通的物理签名,可是使用了公钥加密领域的技术实现.用于鉴别数字信息的方法. 一套数字签名通常定义两种互补的运算.一个用于签名,还有一 ...
- 基于 HTML5 Canvas 的 3D 机房创建
对于 3D 机房来说,监控已经不是什么难事,不同的人有不同的做法,今天试着用 HT 写了一个基于 HTML5 的机房,发现果然 HT 简单好用.本例是将灯光.雾化以及 eye 的最大最小距离等等功能在 ...
- 为 NativeScript 项目添加 iOS / Android 平台 API 的智能感知
使用 NativeScript ,我们可以很容易的调用平台的原生 API,在开发过程中,我们可以添加这些 API 的 d.ts 文件来提供智能感知,帮助我们更方便的构建媲美原生的 APP. 首先通过 ...
- 自学Zabbix3.1-语言切换
题记: 默认使用的zabbix版本为Zabbix 3.0.8 登陆到zabbix web控制台默认是英文界面,对有些英文不好或者习惯中文的人来说会有不适应. 实际上是切换到中文版本,步骤:点击用户 ...
- 阿里云ECS部署ZooKeeper注意事项
如果ECS为专有网络+弹性IP时,配置集群中,"自己"的ip要写成0.0.0.0,其他服务器可以写成公网ip.否则会包如下错误: java.net.BindException: C ...
- JavaSe:代码块执行顺序
//执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. //普通代码块:在方法或语句中出现 ...
- [LeetCode] 二叉树相关题目(不完全)
最近在做LeetCode上面有关二叉树的题目,这篇博客仅用来记录这些题目的代码. 二叉树的题目,一般都是利用递归来解决的,因此这一类题目对理解递归很有帮助. 1.Symmetric Tree(http ...