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}.
/**
* 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的更多相关文章
- [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 (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 ...
- 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 ...
- String reorder
本问题出自:微软2014实习生及秋令营技术类职位在线测试 (Microsoft Online Test for Core Technical Positions) Description For th ...
- 62. 链表重排[Reorder List]
[本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] Given a singly linked list L: L0→L ...
- 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 ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- 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 ...
- 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 ...
随机推荐
- win7下安装mysql
(1)官网下载mysql: http://dev.mysql.com/downloads/mysql/ (2)解压后,进入到该目录下,将my-default.ini文件复制一份改名为my.ini 打开 ...
- 将Discuz.net 集成到asp.net
1,将bbs文件夹拷贝到网站的根目录下: 2,将bbs里面的bin目录,config目录,cache目录,DNT.config文件拷贝到网站根目录下: 3,修改网站web.config文件,在http ...
- 回忆读windows 核心编程
看<windows 核心编程> 第五版到纤程了,下一章节即将介绍内存体系编程.如果做window平台下的开发,我感觉此书一定要读.记得开始讲解了window的基础,然后讲解内核对象.内核对 ...
- 深入理解C语言的函数调用过程
本文主要从进程栈空间的层面复习一下C语言中函数调用的具体过程,以加深对一些基础知识的理解. 先看一个最简单的程序: 点击(此处)折叠或打开 /*test.c*/ #include stdio. ...
- 反射中通过class标记来获取字段及方法
//这是通过class标记获取字段的代码 Field[] fields= classzz.getDeclaredFields(); //获取该class标记的表名代码,必须为,getSimpleNam ...
- ADB工具常用指令和使用情形分析
一.ADB常用命令:(不需要进到设备,也就是:不需要执行adb shell) adb devices 列出所有的已连接的设备 adb kill-server 关闭adb adb start-serve ...
- mysql主主复制(双主复制)配置步骤
以前我们介绍的都是主从复制,这里给各位介绍一个双主复制了,下面都希望两个主服务器数据自动复制的话可参考一下此文章. MySQL主主复制结构区别于主从复制结构.在主主复制结构中,两台服务器的任何一台 ...
- C++字符转码
wchar_t* U8ToUnicode(char* szU8) { //UTF8 to Unicode //由于中文直接复制过来会成乱码,编译器有时会报错,故采用16进制形式 //char* szU ...
- Activity之间的通信
通常Activity之间的通信有三种方式:单向不传参数通信.单项传参数通信和双向通信. 这几种传递方式都需要通信使者Intent.以下将用代码来辅助理解. 1.单向不传递参数通信 public cla ...
- 3.1 SharePreference
SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中,路径为:/data/data/<pac ...