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:首先将原来的链表拆分成两部分,用next和next.next分

2:将第二部分链表反转

3:将第一部分链表和第二部分反转了的链表merge

Takeaway Messages from This Problem

The three steps can be used to solve other problems of linked list. A little diagram may help better understand them.

Reverse List:

Merge List:

下面附上程序源码:

public void reorderList(ListNode head) {if(head == null || head.next==null)  {  return;  }  ListNode walker = head;  ListNode runner = head;  while(runner.next!=null && runner.next.next!=null)  {  walker = walker.next;  runner = runner.next.next;  }  ListNode head1 = head;  ListNode head2 = walker.next;  walker.next = null;  head2 = reverse(head2);  while(head1!=null && head2!=null)  {  ListNode next = head2.next;  head2.next = head1.next;  head1.next = head2;  head1 = head2.next;  head2 = next;  }  }private ListNode reverse(ListNode head)  {  ListNode pre = null;  ListNode cur = head;  while(cur!=null)  {  ListNode next = cur.next;  cur.next = pre;  pre = cur;  cur = next;  }  return pre;  }

  

转载于:https://www.cnblogs.com/gracyandjohn/p/4432402.html

leetcode之Reorder List相关推荐

  1. 【重点】LeetCode 143. Reorder List

    LeetCode 143. Reorder List Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4254860.html 这段代码有值得学习 ...

  2. [LeetCode] 143. Reorder List_Middle tag: Linked 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 mo ...

  3. leetcode day5 -- Reorder List Linked List Cycle II

    1.  Reorder List Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln ...

  4. [Leetcode][JAVA] 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 Solutions : 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 ...

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

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

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

  9. LeetCode 143. Reorder List

    Tag:List Difficulty:Medium Problem 重排链表  给定一个单链表 L 的头节点 head ,单链表 L 表示为:  L0 → L1 → - → Ln - 1 → Ln ...

最新文章

  1. Android学习记录之:Toast的使用
  2. 微信小程序scroll-wiew 横向滚动问题
  3. 20220129CTF刷题-- WEB方向
  4. 2017年度总结:迷茫。
  5. oracle plsql 无法连接 报 ORA-12560: TNS:protocol adapter error
  6. SpringBoot实践 - SpringBoot+MySql+Redis
  7. 小米MIUI光标适配问题
  8. harmonyos2.0三大技术特点,科普干货|漫谈鸿蒙LiteOS-M与HUAWEI LiteOS内核的几大不同...
  9. php 正则匹配 文件,php – 正则表达式匹配.htaccess中的一系列文件类型
  10. 重装jdk时出错Error: could not open `C:\Program Files\Java\jre6\lib\amd64\jvm.cfg'
  11. Matlab实现身份证号码快速识别
  12. qq企业邮箱怎么删除邮件服务器,腾讯企业邮箱如何删除邮件,有什么要注意的呢?...
  13. 洛伦茨曲线_洛伦兹曲线
  14. 计算机之父的童年故事教案,《“计算机之父”的童年故事》教学设计
  15. 克莱斯勒等公司宣布召回缺陷汽车
  16. 【shaderforge学习笔记】 Parallax节点(视差节点)
  17. 醍醐灌顶之-线性代数-矩阵论
  18. obs无法录屏或无法保存视频的解决方法
  19. Vue中的@blur事件
  20. pycharm使用eval reset不能重置

热门文章

  1. OTA升级flash分区
  2. 在ApacheHTTPD服务器中使用DSO完全分析
  3. http header头设置反向代理不缓存
  4. 一个ASP.NET中使用的MessageBox类
  5. GoogleLog(GLog)源码分析
  6. 图像相似度计算之哈希值方法OpenCV实现
  7. python向量化编程_向量化编程思维。
  8. java.io包对象读写_java.io 包中的____________和____________类主要用于对对象(Object)的读写_学小易找答案...
  9. 测试打桩_DNF:CEO实测旭旭宝宝红眼,打桩高达2494E,伤害超越狂人剑魂
  10. Angular应用开发中遇到的问题