一、题目描述

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

二、分析

1、暴力解法

这种解法所需的时间的时间复杂度比较高,为O(n2)

  

代码如下:该代码在Leetcode上提交会提示超时

public void reorderList(ListNode head) {
if(head==null||head.next==null||head.next.next==null){ //当结点的个数小于等于2时,不需要做任何操作。
return ;
}
ListNode rearNode=null; //该指针指向链表的尾结点
ListNode currentNode =head; //前面的结点进过了插入
ListNode preNode=null; //永远指向rearNode结点的前面一个结点
while(currentNode!=null){
rearNode=currentNode;
while(rearNode.next!=null){ //寻找尾结点
preNode=rearNode;
rearNode=rearNode.next;
} if(rearNode!=currentNode){ //当rearNode与currentNode结点相等时,表示已经结束
preNode.next=null;
rearNode.next=currentNode.next;
currentNode.next=rearNode;
currentNode=rearNode.next;
}
else{
break;
}
}
}

2、时间较快的解法,该算法主要分为三个部分:

a、寻找链表的中间结点(midNode),并将链表分一分为二;一个链表的头结点分别为head和newHead;

b、将链表newHead进行反转;

c、将反转后的链表分间隔的插入都head链表中去。

d、时间复杂度为O(n)

代码实现如下:

package com.edu.leetcode;

import com.edu.leetcode.*;

public class ReorderList {

    public void reorderList(ListNode head){
if(head==null||head.next==null||head.next.next==null){ //当结点的个数小于等于2时,不需要做任何操作。
return ;
}
/*
* 第一部分主要是用来寻找链表的中间结点
*/
ListNode midNode=head; //寻找链表的中间结点
ListNode rearNode=head.next; //midNode走一步,readNode走两步
while(rearNode!=null){
rearNode=rearNode.next;
if(rearNode!=null){
midNode=midNode.next;
rearNode=rearNode.next;
}
}
/*
* 第二部是将链表对半分为两个部分,并将后面那个链表进行反转
*/ ListNode newHead=midNode.next;
midNode.next=null;
ListNode curentNode=newHead; //该结点用来指向第一结点,并且永远不需要移动位置
rearNode=curentNode.next; //currentNode后面的一个结点
while(rearNode!=null){ //将currentNode后面的一个结点放到头结点(newHead)的前面
curentNode.next=rearNode.next;
rearNode.next=newHead;
newHead=rearNode;
rearNode=curentNode.next;
} /*
* 第三部分将newHead为头结点的链表依次插入到head链表中
*/ curentNode=head; //head中当前插入的位置
rearNode=newHead; //当前的newHead结点
while(curentNode!=null&&rearNode!=null){ //将rearNode结点插入到curentNode的后面
newHead=rearNode.next; //将newHead重新赋值
rearNode.next=curentNode.next;
curentNode.next=rearNode;
curentNode=rearNode.next;
rearNode=newHead;
} } public static void main(String[] args) {
// TODO Auto-generated method stub
ListNode first1 = new ListNode(0);
ListNode rear1 =first1; for(int i=1;i<10;i++){
ListNode q= new ListNode(i);
rear1.next=q;
rear1=q;
}
ListNode q=first1;
while(q!=null){
System.out.print(q.val+ ",");
q=q.next;
}
System.out.println();
ReorderList rl = new ReorderList();
rl.reorderList(first1); ListNode p=first1;
while(p!=null){
System.out.print(p.val+ ",");
p=p.next;
}
System.out.println(); } }

【Leetcode】Reorder List JAVA的更多相关文章

  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 (middle)

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

  3. 【Leetcode】Sort List JAVA实现

    Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...

  4. 【leetcode】solution in java——Easy4

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6415011.html 16:Invert Binary Tree 此题:以根为对称轴,反转二叉树. 思路:看到 ...

  5. 【leetcode】solution in java——Easy3

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6412505.html 心得:看到一道题,优先往栈,队列,map,list这些工具的使用上面想.不要来去都是暴搜 ...

  6. 【leetcode】solution in java——Easy2

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html 6:Reverse String Write a function that takes ...

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

  8. 【LeetCode】Reorder Log Files(重新排列日志文件)

    这道题是LeetCode里的第937道题. 题目描述: 你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写 ...

  9. 【leetcode】solution in java——Easy5

    转载请注明原文地址: 21:Assign Cookies Assume you are an awesome parent and want to give your children some co ...

随机推荐

  1. Ubuntu 12.04 SSH 安装

    By default Ubuntu Desktop OS comes with ssh clientpackage. It does not include ssh server package wh ...

  2. python list删除元素 del remove

    L=[5,4,3,2,1,'abc'] del 按照index删除比如: del L[i] del L[i:j] remove按照内容删除 L.remove('abc') L.remove(0)#会报 ...

  3. PHP 投票练习

    重点:1.进度条的显示2.操作数据库<form action="chuli.php" method="post"> <?php include ...

  4. [原]poj-3009-Curling 2.0-dfs

    题目太长就不贴了,题意: 上下左右四联通块,2表示起点,3表示终点,1为block,0为空地,每动一次冰壶,冰壶就会向推动的方向一直移动,直到碰到block或出界,如果碰到block就在block前停 ...

  5. AOJ 2170 Marked Ancestor (基础并查集)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...

  6. Python代码编辑器

    PyCharm Community 说到PyCharm Community,我们就会想到它是一款免费的开源Python代码编辑器,不过这也是事实啦.PyCharm Community为我们提供了轻量级 ...

  7. html下select追加元素,IE下错误

    var selectCtr=window.document.getElementById("lesson_up"); selectCtr.add(opt,selectCtr.opt ...

  8. nginx.conf 配置文件详解

    简单的实现nginx在前端做反向代理服务器的例子,处理js.png等静态文件,jsp等动态请求转发到其它服务器tomcat: user www www; worker_processes ; erro ...

  9. leetcode:Reverse Linked List

    Reverse a singly linked list. 代码如下: the iterative solution:(c++) /** * Definition for singly-linked ...

  10. plsql programming 17 过程, 函数与参数

    代码模块化, 即将一大块代码拆成若干小块(过程), 然后就可以在其他模块调用这些模块了, 这样, 重用性更好, 也方便管理. 过程: 过程是一个可以像执行 PL/SQL 语句一样调用的程序, 一个过程 ...