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 ...
随机推荐
- BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)
蛋蛋用链剖A的,我写的LCT 3282: Tree Time Limit: 30 Sec Memory Limit: 512 MB Submit: 1241 Solved: 542 [Submit][ ...
- 多线程练习(java)
public class TestThread { public static void main(String[] args) { RandomNumber r=new RandomNumber() ...
- UvaLive 5026 Building Roads
传送门 Time Limit: 3000MS Description There is a magic planet in the space. There is a magical country ...
- android jni
1, java.lang.UnsatisfiedLinkError: Couldn't load xxxxx: findLibrary returned null 当 apk 是被放到 /system ...
- VirtualBox:Fatal:Could not read from Boot Medium! System Halted解决措施
打开VirtualBox加载XP虚拟机操作系统时,出现含有下面文字的错误: Could not read from Boot Medium! System Halted 或下面图中所示错误: ...
- ifconfig 工具
ifconfig 工具 ifconfig 命令常用格式: 格式:ifconfig显示当前激活的网络接口信息. 格式:ifconfig {INTERFACE}显示指定网络接口的信息.比如:eth0, e ...
- IIS 解决问题:HTTP 错误 401.1 - 未授权:登录失败
解决问题:HTTP 错误 401.1 - 未授权:登录失败 HTTP 错误 401.1 - 未授权:登录失败 Internet 信息服务 -----------解决这个问题,折磨了两天,终于搞定了,首 ...
- ftp (文件传输协议)
ftp (文件传输协议) 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议” ...
- 把一个一维数组转换为in ()
把一个一维数组转换为in()形式. function dbCreateIn($itemList) { if(empty($itemList )){ return " IN ('') &quo ...
- oracle数据库备份与还原
一 关于expdp和impdp 使用EXPDP和IMPDP时应该注意的事项:EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.EXPDP和IMPDP是服务端的工具程 ...