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 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相关推荐
- 【重点】LeetCode 143. Reorder List
LeetCode 143. Reorder List Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4254860.html 这段代码有值得学习 ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- LeetCode 143. Reorder List
Tag:List Difficulty:Medium Problem 重排链表 给定一个单链表 L 的头节点 head ,单链表 L 表示为: L0 → L1 → - → Ln - 1 → Ln ...
最新文章
- Android学习记录之:Toast的使用
- 微信小程序scroll-wiew 横向滚动问题
- 20220129CTF刷题-- WEB方向
- 2017年度总结:迷茫。
- oracle plsql 无法连接 报 ORA-12560: TNS:protocol adapter error
- SpringBoot实践 - SpringBoot+MySql+Redis
- 小米MIUI光标适配问题
- harmonyos2.0三大技术特点,科普干货|漫谈鸿蒙LiteOS-M与HUAWEI LiteOS内核的几大不同...
- php 正则匹配 文件,php – 正则表达式匹配.htaccess中的一系列文件类型
- 重装jdk时出错Error: could not open `C:\Program Files\Java\jre6\lib\amd64\jvm.cfg'
- Matlab实现身份证号码快速识别
- qq企业邮箱怎么删除邮件服务器,腾讯企业邮箱如何删除邮件,有什么要注意的呢?...
- 洛伦茨曲线_洛伦兹曲线
- 计算机之父的童年故事教案,《“计算机之父”的童年故事》教学设计
- 克莱斯勒等公司宣布召回缺陷汽车
- 【shaderforge学习笔记】 Parallax节点(视差节点)
- 醍醐灌顶之-线性代数-矩阵论
- obs无法录屏或无法保存视频的解决方法
- Vue中的@blur事件
- pycharm使用eval reset不能重置
热门文章
- OTA升级flash分区
- 在ApacheHTTPD服务器中使用DSO完全分析
- http header头设置反向代理不缓存
- 一个ASP.NET中使用的MessageBox类
- GoogleLog(GLog)源码分析
- 图像相似度计算之哈希值方法OpenCV实现
- python向量化编程_向量化编程思维。
- java.io包对象读写_java.io 包中的____________和____________类主要用于对对象(Object)的读写_学小易找答案...
- 测试打桩_DNF:CEO实测旭旭宝宝红眼,打桩高达2494E,伤害超越狂人剑魂
- Angular应用开发中遇到的问题