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 ...
随机推荐
- 初步认识JUnit
初步认识JUnit 目前大多数的基于Java的企业应用软件,肯定少不了单元测试,程序员通过编写单元测试来验证自己程序的有效性:管理者通过持续自动的执行单元测试和分析单元测试覆盖率来确保软件本身的质量. ...
- .Net中几种常见的页面跳转传值方法
1.ASP Server对象Execute方法 ASP Server对象的Execute方法可以在执行当前页面的过程中将另一个页面执行结果的内容插入到当前页面的输出中.Execute方法带一个参数,是 ...
- python学习笔记-Day6(2)
xml处理模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融 ...
- XE3随笔3:访问
测试数据提前加入 Memo1 中: { "name": "张三", /* 注释 */ "age": 33, "sex": ...
- Daily Scrum 12.9
今日完成任务: 修复了提交回答,自动消除换行符,导致文本显示混乱的BUG.解决个人信息修改界面中,问题显示顺序不对的BUG.基本完成数据库接口webservice工作.但引入的异常还未修复. 遇到困难 ...
- rocksdb编译测试的正确姿势
需要先安装 gflags 在进行 make db_bench 不然运行 db_bench 会出现 Please install gflags to run rocksdb tools 错误 bench ...
- 解决AndroidADT自带Eclipse编辑器不能自动代码提示的问题
今天发现,我下载的AndroidADT开发套装中自带的Eclipse没有自动代码提示功能.通过参考http://blog.csdn.net/coolszy/article/details/724195 ...
- who is the middle
Description FJ is surveying his herd to find the most average cow. He wants to know how much milk th ...
- List的使用
List<string> AllFilesPath = new List<string>(); ) // get all files path { ; i < files ...
- 利用Apache Ant编译Hadoop2.6.0-eclipse-plugin
环境要求:系统不重要,重要的是要有Ant环境,这里不做赘述,自行百度配置去. 1)在github上下载Hadoop-eclipse-plugin-master的zip包,下载地址. 2)在Hadoop ...