/**
* 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的更多相关文章

  1. [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 ...

  2. [leetcode]Reorder List @ Python

    原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. LeetCode Reorder List

    struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution ...

  7. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  9. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  10. 【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 ...

随机推荐

  1. linux 安装 cenos7 和 jdk

    安装一个虚拟机安装cenos7 版本的 安装完虚拟机后必备工具 第一步. 配置网路 设置桥接网路设置静态网络参考 下面博文 http://www.cnblogs.com/Jerry1104/p/758 ...

  2. Codeforces 482B Interesting Array

    题意:构造一个长度为n的序列,使其满足m个形式如下如下约束:a[l]&a[l+1]&a[l+2]&....&a[r]=q 从Dalao的博客上看到这题,决定去水水.做法 ...

  3. FPGA编程基础(一)--參数传递与寄存器使用

    一.參数映射 參数映射的功能就是实现參数化元件.所谓的"參数化元件"就是指元件的某些參数是可调的,通过调整这些參数从而可实现一类结构类似而功能不同的电路.在应用中.非常多电路都可採 ...

  4. Android研究之监听自身应用被卸载代码实现

    1.通过jni实现函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...

  5. ES5规范之Object增强

    在ES5规范中.另一个比較重要的改进,就是Object对象的增强.ES5为Object新增了一系列函数.用于编写安全健壮的程序,今天我们就来一一介绍它们的用法. 以下就是ES5中Object新增的函数 ...

  6. AJAX跨域问题解决---后台解决

    对于ajax请求数据,经常出现一个坑,防不胜防.今天突然找到一个很好的解决办法,直接在后台设置资源共享就可以了. 代码为:response.raw().setHeader("Access-C ...

  7. Shiro学习(一)总体介绍

    1.1  简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Securi ...

  8. Spring学习笔记(三)之装配Bean

    除了组件扫描与自动装配之外还有基于Java代码的装配与基于XML的装配. 有一些场景是我们不能用自动装配的,比如我们要给第三方库中的组件装配到我们的应用中,这时自动装配无效,因为自动装配只能扫描本应用 ...

  9. intellij idea 在什么地方打开终端Terminal

    File→Plugins→Terminal 勾选它,点击Apply,点击Restart即可如果是要启动terminal则必须先做完上面动作,才可以点击Tools→Open Terminal... 来达 ...

  10. #ifdef #else #endif #if #ifndef 的用法

    预编译就是在对源文件进行处理之前(如在语法扫描和分析之前),先处理预处理部分,精简代码,然后再进行编译. 预处理命令有:#include 文件包含.#define 宏定义.以及要重点讲的#if.#if ...