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

代码如下:

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public void reorderList(ListNode head) {
  11.  
  12. try{
  13. if(head.next.next!=null){
  14. ListNode p=head;
  15.  
  16. Map<ListNode,ListNode> map=new HashMap<>();
  17.  
  18. while(p.next.next!=null)
  19. {
  20. map.put(p.next,p);
  21. p=p.next;
  22. }
  23. ListNode q=head;
  24. ListNode temp=p.next;
  25.  
  26. while(q.next!=p.next&&q.next!=null)
  27. {
  28. p.next=temp.next;
  29. temp.next=q.next;
  30. q.next=temp;
  31. q=temp.next;
  32.  
  33. temp=p;
  34. p=map.get(p);
  35. }
  36.  
  37. }
  38.  
  39. }catch(NullPointerException e){}
  40.  
  41. }
  42. }

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. bzoj 2324: [ZJOI2011]营救皮卡丘

    #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #inclu ...

  2. POJ 2828 单点更新(好题)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15086   Accepted: 7530 Desc ...

  3. 在虚拟机上安装Ubutu完成后卡在VM Tool的安装上

    今天在虚拟机上装Ubuntu之后,卡在了VM Tool的安装页,点击回车后可以进入命令行模式.并出现如下提示“Vmware Easy Install PLEASE WAIT! VMware Tools ...

  4. Apache—DBUtils

    简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影 ...

  5. RPI学习--WebCam_mplayer

    1,安装mplayer $ sudo apt-get install mplayer 2,运行 $ sudo mplayer tv:// 有时会秀逗,绿屏,多试几下就好了,情况未知

  6. HTML--11marquee标签

    页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...

  7. 收藏的博客--Ogre

    1.Ogre一些东西和流程深入讲解  http://blog.csdn.net/yanonsoftware/article/category/226048 2.Ogre天龙八部分析 http://bl ...

  8. eclipse安卓模拟器窗口大小调整

    引自百度经验的链接: http://jingyan.baidu.com/article/3aed632e18c7e97011809161.html

  9. Linux物理内存相关数据结构

    节点:pg_data_t typedef struct pglist_data { zone_t node_zones[MAX_NR_ZONES]; zonelist_t node_zonelists ...

  10. Java中接口作为方法的返回

    在<算法>中的散列表一节,在用拉链法实现散列表的API时要求实现以下一个方法: public Iterable<Key> keys() 我们知道Iterable是一个接口,那么 ...