题目:

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. 高频交易策略之Penny Jump[z]

    高频交易策略之Penny Jump 今天假设有一个笨笨的大型机构投资人(共同基金,银行,退休基金....),他想要买进一只股票,但又不想挂市价买进,所以就在市场里面挂了一张要买进的大单.这时候所有市场 ...

  2. MySQL-->基础-->001-->MySQL基本操作

    一.MySQL安装 卸载mysql 第一步 sudo apt-get autoremove --purge mysql-server-5.0 sudo apt-get remove mysql-ser ...

  3. db2 进程

    DB2 通用数据库进程全接触 Dwaine Snow, DB2 UDB 和 Informix IDS 产品经理, IBM 多伦多实验室 Dwaine Snow 是 DB2 UDB 分区数据库以及查询巡 ...

  4. Vmware虚拟机克隆的网卡问题

    系统环境:red hat 6.4 在虚拟机上使用克隆后,克隆机没有eth0, 出现eth1并且出错No suitable device found: no device found for conne ...

  5. xcode添加音效

    xcode添加背景音乐/音效 背景音乐:http://www.cnblogs.com/jiayongqiang/p/5476069.html 音效: 一.介绍: 又称“短音频”,通常在程序中的播放时长 ...

  6. spring随想

    //不定时持续更新 1.拦截器通过配置文件,在某方法前后添加一些处理,如权限判断等,减少了改方法需要处理的事,是其更专注,由配置文件来设定责任链,更灵活,而且责任链能够复用(一方面是这样能由sprin ...

  7. 解决VS2010链接错误:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    网上有多种解决办法,我用最懒的一种,系统是Win7 X86,方法如下: C:\Windows\Microsoft.NET\Framework\v4.0.30319\cvtres.exe C:\Prog ...

  8. hdu 2586(LCA在线ST)

    How far away ? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): A ...

  9. MyBatis学习之路之configuration配置

    1.首先讲解的是MyBatis核心配置文件configuration.xml的配置 一个完整的configuration.xml配置顺序如下: properties,settings,typeAlia ...

  10. 【整理】--VC 编译整理

    .h用于编译阶段的审核,如在math.h中有函数声明:int abs(int);但是在使用中写为#include <math.h>...abs(3,5);编译器阶段就会检测出错误. .dl ...