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 ...
随机推荐
- 【kAri OJ】wzt的树
时间限制 1000 ms 内存限制 65536 KB 题目描述 改革春风吹满地,中国人民真争气!家庭联产承包责任制以后,全国人民争想发家致富.wzt于是包了一个山头来种植金丝楠木,花了好几年种了N棵树 ...
- 洛谷P1136 迎接仪式
题目描述 LHX教主要来X市指导OI学习工作了.为了迎接教主,在一条道路旁,一群Orz教主er穿着文化衫站在道路两旁迎接教主,每件文化衫上都印着大字.一旁的Orzer依次摆出“欢迎欢迎欢迎欢迎……”的 ...
- eclipse中使用git
有的eclipse已经自带了Git了,就不用安装了.如果,想重新安装,可以先卸载GIT,卸载 不同eclipse卸载不一样: 1.在Eclipse中依次点击菜单"Help"-> ...
- MongoDB之bson的介绍
MongoDB之bson的介绍 1. 什么是bson BSON是一种类json的一种二进制形式的存储格式,简称Binary JSON,它和JSON一样,支持内嵌的文档对象和数组对象,但是BSON有JS ...
- SpringServletContext简单案例
一.项目结构及相应jar包,如下图 二.UserService代码 package com.hjp.service; /** * Created by JiaPeng on 2015/11/15. * ...
- 用Nikto探测一个网站所用到的技术
Nikto是一款开源的(GPL)网页服务器扫描器,它可以对网页服务器进行全面的多种扫描,包含超过3300种有潜在危险的文件/CGIs:超过 625种服务器版本:超过230种特定服务器问题,包括多种有潜 ...
- UIScrollview自动布局,UIScrollviewAutolayoutDemo
参考文档:http://www.cocoachina.com/ios/20150104/10810.html UIScrollviewAutolayoutDemo地址:http://pan.baidu ...
- IE input file隐藏不能上传文件解决方法
当大神们都在探讨更深层次的问题时,我还在这里转载发些肤浅的问题解决方案.罢了,为了和我一样笨的后来人. 问题: 上传文件时,用<input type="file" /> ...
- Swift语言简介+快速上手
相关: Xcode 6 beta:https://developer.apple.com/xcode/downloads/ swift语言学习文档英文版:http://pan.baidu.com/s/ ...
- linux下的视频音频播放器终极解决方案
要使用(启用)rpmfusion, 一定要先启用enable epel包: Important notes You need to enable EPEL on RHEL 5 & 6 or c ...