LeetCode解题报告:Reorder List
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.利用快慢两个指针将链表一分为二;
2.针对第二个子链表求倒序;
3.利用merge思想将两个子链表合并。
代码:
public class ReorderSingleList {
/**
* Definition for singly-linked list. class ListNode { int val; ListNode
* next; ListNode(int x) { val = x; next = null; } }
*/
public void reorderList(ListNode head) {
if (head == null || (head.next == null)) {
return;
}
// fast and slow point find the mid position.
ListNode fast = head;
ListNode slow = head;
while ((fast != null) && (fast.next != null)) {
fast = fast.next.next;
slow = slow.next;
} // reverse the last second list.
ListNode headnode = new ListNode(-1);
headnode.next = slow;
ListNode temp = headnode.next;
while (temp.next != null) {
ListNode insert = temp.next;
ListNode currNext = insert.next;
insert.next = headnode.next;
headnode.next = insert;
temp.next = currNext;
} // merge insert
ListNode firstcur = head;
ListNode secondcur = headnode.next;
while (firstcur != slow && (secondcur != slow)) {// at first,I make a mistake in here;
ListNode firstnex = firstcur.next;
ListNode secondnex = secondcur.next;
firstcur.next = secondcur;
secondcur.next = firstnex;
firstcur = firstnex;
secondcur = secondnex;
}
} public static void main(String[] args) {
ListNode t5 = new ListNode(5);
ListNode t4 = new ListNode(4, t5);
ListNode t3 = new ListNode(3, t4);
ListNode t2 = new ListNode(2, t3);
ListNode t1 = new ListNode(1, t2);
new ReorderSingleList().reorderList(t1);
System.out.println(t1);
} }
LeetCode解题报告:Reorder List的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
- LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
- LeetCode解题报告—— 1-bit and 2-bit Characters & 132 Pattern & 3Sum
1. 1-bit and 2-bit Characters We have two special characters. The first character can be represented ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
随机推荐
- java 中能否使用 动态加载的类(Class.forName) 来做类型转换?
今天同事提出了一个问题: 将对象a 转化为类型b,b 的classpath 是在配置文件中配置的,需要在运行中使用Class.forName 动态load进来,因为之前从来没有想过类似的问题,所以懵掉 ...
- Android(java)学习笔记192:SQLite数据库(表)的创建 以及 SQLite数据库的升级
一.数据库的创建 1.文件的创建 //引用,如果文件不存在是不会创建的 File file = new File("haha.txt"): //输出流写数据 ...
- [转载]js中__proto__和prototype的区别和关系
首先,要明确几个点:1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性_ ...
- Linux下安装GAMS建模优化工具
1.下载GAMS wget http://d37drm4t2jghv5.cloudfront.net/distributions/24.5.6/linux/linux_x64_64_sfx.exe 2 ...
- 安卓Intent(隐式)
相对于显式Intent(明确指出了目标组件名称的Intent),隐式Intent,没有明确指出目标组件名称.显式Intent直接用组件的名称定义目标组件,这种方式很直接.但是由于开发人员往往并不清楚别 ...
- HTML+CSS基础学习笔记(8)
一.水平居中设置--行内元素 如果设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置text-align:center来实现的 二.水平居中设置 --定宽块状元素 #当被设置元素为块状元素时 ...
- WPF中窗口控件的跨线程调用
在WinForm中,我们要跨线程访问窗口控件,只需要设置属性CheckForIllegalCrossThreadCalls = false;即可. 在WPF中要麻烦一下,同样的不允许跨线程访问,因为没 ...
- Tomcat-java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
好些天没弄java了,今天开MyEclipse,发现启动Tomcat的时候发错了,后来发现,报错如题. 解决方案是将 bin/tomcat-juli.jar 添加到add tomcat classpa ...
- 通过调整表union all的顺序优化SQL
操作系统:Windows XP 数据库版本:SQL Server 2005 今天遇到一个SQL,过滤条件是自动生成的,因此,没法通过调整SQL的谓词达到优化的目的,只能去找SQL中的“大表”.有一个视 ...
- Windows服务安装方法
操作系统:Win8.1 安装方法:在命令行窗口中输入:InstallUtil service.exe 出错原因:需要以管理员身份启动命令行.