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 ...
随机推荐
- iOS开发中常用的手势---边缘手势
说明:以下方法是开发中使用的方法,有什么不对的或者好的方法,请多多指教! 此处的边缘手势是用来控制左侧抽屉视图的弹出以及收回. 添加手势 : 页面上有多个手势时需要遵循 UIGestureRecogn ...
- Vmare12(虚拟机)安装Mac OS X Yosemite 10.10
需要预备的软件如下: OSX10.10的系统镜像,下载好之后将后缀.cdr改成.iso,下载来源如下: 链接:http://pan.baidu.com/s/1sj4ri5R 密码:y86w un ...
- 基于PHP的cURL快速入门
cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性,以及在PHP中如 ...
- css3之@font-face---再也不用被迫使用web安全字体了
1,@font-face 的出现在没有css3之前,我们给网页设计字体只能设置web安全字体,使得我们的网页表现看上去好像都是那个样子,那么如果我们想给字体设置web设计者提供的字体呢?没有问题,cs ...
- js 的执行过程
step 1. 读入第一个代码块. step 2. 做语法分析,有错则报语法错误(比如括号不匹配等),并跳转到step5. step 3. 对var变量和function定义做"预编译 ...
- java_反射_及其简单应用(2016-11-16)
话不多说直接上代码 接口: package bean; /** * user接口 */ public interface User { public String getName(); public ...
- [Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟
赌徒赢得机会有多大? public class Gambler { public static void main(String[] args) { // Run T experiments that ...
- (转)log4j日志级别设置成DEBUG时输出Html代码等问题:
log4j日志级别设置成DEBUG时输出Html代码等问题: 问题: log4j日志级别设置成DEBUG时会输出很多信息,包括一些Html代码 解决方案: log4j的控制是树形,所以在log4j.p ...
- [WinJS] Promise 用法
初学 WinJS, 可能对 Promise 的用法不大清楚,本文简要说明一下 WinJS中 promise 的基本用法. 主要参考自:http://try.buildwinjs.com/#promis ...
- SuperSocket与Netty之实现protobuf协议,包括服务端和客户端
今天准备给大家介绍一个c#服务器框架(SuperSocket)和一个c#客户端框架(SuperSocket.ClientEngine).这两个框架的作者是园区里面的江大渔. 首先感谢他的无私开源贡献. ...