/**
* 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. php计算多个集合的笛卡尔积实例详解

    笛卡尔积 笛卡尔积是指在数学中,两个集合X和Y的笛卡尔积(Cartesian product),又称直积,表示为X*Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员. 假设集合A ...

  2. php面向对象(OOP)编程完全教程(转载笔记,有兴趣可以看看))

    http://www.cnblogs.com/xiaochaohuashengmi/archive/2010/09/10/1823042.html

  3. python-02 数据类型、字符编码、文件处理

    标准数据类型 Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) 数字 #整型 ...

  4. Android关于AutoService、Javapoet讲解

    一.上篇文章提到自定义processor中用到AutoService 文章中我们用到了AutoService, 使用@AutoService(Processor.class),编译后 MethodSp ...

  5. Django模型中value函数运用

    values(*fields) 这个方法返回的是ValuesQuerySet,是QuerySet 的子类,也就是说,你可以用QuerySet里的方法. 需要注意的是,返回的不是list,不要直接当li ...

  6. ROC和AUC的区别

    ROC是一个曲线,AUC是曲线下面的面积值.   ROC曲线是FPR和TPR的点连成的线. 可以从上面的图看到,横轴是FPR, 纵轴是TPR (TPR = TP / (TP + FN):FPR = F ...

  7. response与request回顾学习

    一.response response是servlet.service方法的一个参数,它的类型是javax.servlet.http.HttpServletResponse,在客户端每发出一个请求时, ...

  8. Elasticsearch全文搜索——adout

    现在尝试下稍微高级点儿的全文搜索——一项传统数据库确实很难搞定的任务. 搜索下所有喜欢攀岩(rock climbing)的雇员: curl -XGET 'localhost:9200/megacorp ...

  9. 第一安装oracle数据库后,需要创建一个用户,给用户解锁并赋予权限

    1.第一次安装oracle数据库应该做的事情. 注: 1.安装oracle后需要创建用户,连接数据库,(注意数据库名,还有好像后面的 ":"也有影响) 2.解锁用户, 3.授予新登 ...

  10. 配置nginx支持path_info模式

    简介:我们用thinkphp,CodeIgniter框架的时候,地址基本都是IP/index.php/group_controller?***的模式,通过index.php入口访问php文件 这种模式 ...