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 this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}
, reorder it to {1,4,2,3}
.
解题思路一:
每次将Ln换到前面,得到L0→Ln→L1→L2→L3→,然后对L1使用相同操作,JAVA实现如下:
public void reorderList(ListNode head) {
ListNode headCopy = head;
while (headCopy != null && headCopy.next != null
&& headCopy.next.next != null) {
ListNode temp = headCopy;
while (temp.next.next != null)
temp = temp.next;
temp.next.next = headCopy.next;
headCopy.next = temp.next;
temp.next = null;
temp = headCopy.next.next;
headCopy=headCopy.next.next;
}
}
结果TLE
解题思路二:
空间换时间,将所有的node存到一个list中,然后每次操作list头尾两个node即可,JAVA实现如下:
public void reorderList(ListNode head) {
LinkedList<ListNode> list = new LinkedList<ListNode>();
ListNode headCopy = head,end = head;
while (headCopy != null) {
list.add(headCopy);
headCopy = headCopy.next;
}
while(list.size()>2){
headCopy=list.poll();
end=list.get(list.size()-1);
list.remove(list.size()-1);
headCopy.next=end;
end.next=list.peek();
list.get(list.size()-1).next=null;
}
}
Java for LeetCode 143 Reorder List的更多相关文章
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- 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 ...
- 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 ...
- [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 ...
- Java实现 LeetCode 143 重排链表
143. 重排链表 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...
- Leetcode#143 Reorder List
原题地址 先把链表分割成前后两半,然后交叉融合 实践证明,凡是链表相关的题目,都应该当成工程类题目做,局部变量.功能函数什么的随便整,代码长了没关系,关键是清楚,不容易出错. 代码: ListNode ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 143. Reorder List - LeetCode
Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- HDU #3333
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descript ...
- 轻量级应用开发之(01)第一个IOS程序
一 IPhone轻量级开发 1. 开发环境 Mac 版本: OS X EICap 10.11.3 (15D21) XCode开发版本: Version 7.2.1 (7C1002) 2.简单分析 UI ...
- iOS代码工具箱再续
if (CGRectContainsPoint(self.menuView.frame, point)) { point = [self.view convertPoint:point toView ...
- MVC执行顺序
MVC在底层和传统的asp.net是一致的,在底层之上,相关流程如下: 1)Global.asax里,MvcApplication对象的Application_Start()事件中,调用 RouteC ...
- Linux目录结构及常用命令(转载)
一.Linux目录结构 你想知道为什么某些程序位于/bin下,或者/sbin,或者/usr/bin,或/usr/sbin目录下吗?例如,less命令位于/usr/bin目录下.为什么没在/bin中,或 ...
- Hibernate配置问题
Hibernate是对持久化对象操作,生成SQL语句达到操作数据库目的. 1.Hibernate可以通过两种方式来配置 (1).hibernate.cfg.xml,在此文件里hibernate-con ...
- IOS学习笔记—苹果推送机制APNs
转自:唐韧_Ryan http://blog.csdn.net/ryantang03/article/details/8482259 推送是解决轮询所造成的流量消耗和 电量消耗的一个比较好的解决方案, ...
- 锋利的jQuery-2--一个显示和隐藏的例子,主要看写法
例子:如图,默认不显示全部,点击按钮来回切换,全部显示是一部分推荐的品牌高亮. $(function(){ //dom加载完再执行 var category = $('ul li:gt(5):not( ...
- SpringMVC 返回JSON数据
首先添加json包
- Javascript的调试利器:Firebug使用详解
转载自:http://blog.csdn.net/tianxiaode/archive/2007/09/02/1769152.aspx 一直在用firebug,可是没有这么精通,今天看到本文章觉得 ...