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) { try{
if(head.next.next!=null){
ListNode p=head; Map<ListNode,ListNode> map=new HashMap<>(); while(p.next.next!=null)
{
map.put(p.next,p);
p=p.next;
}
ListNode q=head;
ListNode temp=p.next; while(q.next!=p.next&&q.next!=null)
{
p.next=temp.next;
temp.next=q.next;
q.next=temp;
q=temp.next; temp=p;
p=map.get(p);
} } }catch(NullPointerException e){} }
}

143. Reorder List的更多相关文章

  1. leetcode 143. Reorder List 、86. Partition List

    143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...

  2. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 143. Reorder List - LeetCode

    Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...

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

  5. leetcode 143. Reorder List ----- java

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

  6. Leetcode 143. Reorder List(Medium)

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

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

  8. [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 may not mod ...

  9. 【Leetcode】143. Reorder List

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

随机推荐

  1. UVa 10253 - Series-Parallel Networks

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. KStar ----BPM应用框架,K2 的新星

    “KStar”是基于K2 BPM搭建的应用框架产品,将K2最佳实践方案以产品的形式呈现给用户,该框架面向SOA服务,便于二次开发和扩展,流程设计.用户组织.业务表单.流程管理.系统集成等开发工作,都按 ...

  3. Android Support ;v4、v7、v13的区别

    google提供了Android Support Library package系列的包来保证来高版本sdk开发的向下兼容性,即我们用4.x开发时,在1.6等版本上,可以使用高版本的有些特性,如fra ...

  4. OpenCV之响应鼠标(二):函数cvSetMouseCallback()和其副程式onMouse()的使用(OpenCV2.4.5)

    每當滑鼠在視訊視窗介面點擊一下的時候,都會有固定三個動作 1.點擊(Click) 2.放開(Down)3.滑動(move) 因此,程式執行滑鼠在點擊的時候onMouse()都會連續跑三次,代表滑鼠在點 ...

  5. iOS开发中XML的DOM和SAX解析方法

    一.介绍 dom是w3c指定的一套规范标准,核心是按树形结构处理数据,dom解析器读入xml文件并在内存中建立一个结构一模一样的“树”,这树各节点和xml各标记对应,通过操纵此“树”来处理xml中的文 ...

  6. 捕获异常的两种方式Exception

    1.抛出异常:让调用此方法的代码去管 public static void GetFile() throws Exception{} package com.throwable; import jav ...

  7. CGAffineTransformMakeTranslation和CGAffineTransformTranslate

    分类: ios基础2013-01-06 22:05 15513人阅读 评论(2) 收藏 举报 1.CGAffineTransformMakeTranslation每次都是以最初位置的中心点为起始参照 ...

  8. C++实现python标准库中的Counter

    看python standard library by exmple里面提到一个Counter容器,它像muliset一样,能够维持一个集合,并在常量时间插入元素.查询某个元素的个数,而且还提供了一个 ...

  9. linux基础命令学习(六)DHCP服务器配置

    工作原理:        1.客户机寻找服务器:广播发送discover包,寻找dhcp服务器        2.服务器响应请求:单播发送offer包,对客户机做出响应.提供客户端网络相关的租约以供选 ...

  10. 排版字号对应多少pt

    各字号对应多少pt?初号= 42pt: 小初号= 36pt: 一号= 26pt: 二号= 22pt: 小二号= 18pt: 三号= 16pt: 四号= 14pt: 小四号= 12pt: 五号= 10. ...