题目:

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→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}.

基本思路:     用了一个很蠢很直接的方法,定位到中间节点,后半段入栈,然后把后半段与前半段拼接组成新链表
 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) { if (head == null)
return; ListNode node = head.next;
int len = 0;
while(node != null){
len ++;
node = node.next;
} System.out.println("len:"+len); if(len == 2){
ListNode temp = head.next; head.next = temp.next;
temp.next = null;
head.next.next = temp;
return;
}else if(len == 1 || len == 0){
return;
} int isOdd = len % 2;
if(isOdd == 1){
int mid = len / 2 + 1;
Stack<ListNode> stack = new Stack(); int j = 1;
ListNode midNode = head.next;
while(j != mid){
j++;
midNode = midNode.next;
} ListNode realMidNode = midNode;
midNode = midNode.next;
j++;
stack.push(midNode);
while(j != len){
j++;
midNode = midNode.next;
stack.push(midNode);
} j = 1; ListNode leftTemp = head.next;
ListNode rightTemp = stack.pop();
rightTemp.next = leftTemp;
head.next = rightTemp;
ListNode tail = leftTemp;
j++;
while(j < mid){
j++;
leftTemp = leftTemp.next;
rightTemp = stack.pop();
rightTemp.next = leftTemp;
tail.next = rightTemp;
tail = leftTemp;
} tail.next = realMidNode;
realMidNode.next = null; }else{
int mid = len / 2 ;
Stack<ListNode> stack = new Stack<>(); int j = 1;
ListNode midNode = head.next;
while(j != mid){
j++;
midNode = midNode.next;
} j++;
midNode = midNode.next;
stack.push(midNode); while(j != len){
j++;
midNode = midNode.next;
stack.push(midNode);
} j=1; ListNode leftTemp = head.next;
ListNode rightTemp = stack.pop();
rightTemp.next = leftTemp;
head.next = rightTemp;
ListNode tail = leftTemp; while(j != mid){
j++;
leftTemp = leftTemp.next;
rightTemp = stack.pop();
rightTemp.next = leftTemp;
tail.next = rightTemp;
tail = leftTemp;
}
tail.next = null;
} }
}
运行结果

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 (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. 13. Reorder List

    Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Y ...

  4. String reorder

    本问题出自:微软2014实习生及秋令营技术类职位在线测试 (Microsoft Online Test for Core Technical Positions) Description For th ...

  5. 62. 链表重排[Reorder List]

    [本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] Given a singly linked list L: L0→L ...

  6. Java for LeetCode 143 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 ...

  7. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  8. Reorder List [LeetCode]

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

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

随机推荐

  1. 行锁sqlserver

    SELECT COUNT(1) FROM BLBQ_Sys_TableId With (RowLock,UpdLock) WHERE Table_Name = @Table_Name commit   ...

  2. linux shell 脚本攻略学习19--sed命令详解

    sed(意为流编辑器,英语“stream editor”的缩写)是Unix/linux常见的命令行程序.sed用来把文档或字符串里面的文字经过一系列编辑命令转换为另一种格式输出,即文本替换.sed通常 ...

  3. IOS网络请求原理

    1,为什么要用到网络,  只有通过网络跟外界进行数据交互,数据更新,应用才能保持哦新鲜.活力 如果没有网络,也就缺少数据变化.变成一滩死水. 2, 良好的移动网络应用 = 良好的UI + 良好的用户体 ...

  4. Sublime Text 3常用快捷键 以及package control 安装

    网络上搜索到的,现整理下来备用: package control安装 view下 ctrl+~ import urllib.request,os; pf = 'Package Control.subl ...

  5. hdoj 1874 畅通工程续

    Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行 ...

  6. 【洛谷P1541】乌龟棋

    四维dp #include<cstdio> #include<cstring> using namespace std; ; ],a,b,c,d,n,m; int max(in ...

  7. JS-定时器换背景

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...

  8. tesseract-ocr 出现 错误 Please make sure the TESSDATA_PREFIX environment variable is set to the parent d irectory of your "tessdata" directory.解决方案

    简单就是说把tessdata拷贝到exe的所在目录,或者设置TESSDATA_PREFIX环境变量

  9. javaweb-url /

    /一直搞得不清不楚 有时候不用加有时加了也行,有时必须加 转发自XXX论坛 推荐使用 <% String path = request.getContextPath(); String base ...

  10. Spring <context:annotation-config/> 解说

    在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册 AutowiredA ...