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 ...
随机推荐
- static inner class 什么时候被加载
一直认为在加载outer class 的同时也会加载inner class 并且完成静态变量和代码块的初始化,今天在维基百科上面看到 “The static class definitionLazyH ...
- ubuntu中安装eclipse 分类: android ubuntu linux 学习笔记 2015-07-07 10:19 75人阅读 评论(0) 收藏
上一篇说了安装jdk的事,于是趁热打铁,决定把eclipse也安装了. 下载这一系列就不用说了. 下载完成之后: 然后解压,解压之后文件剪切到/usr/software文件夹中,同时重命名为eclip ...
- portal开发"下拉框"“日期框”查询要怎么配置
下面的这些是我今天的成果! 总的来说是一步一步摸索出来的!还是等感谢超哥的耐心指导,犯了一些错误! 1.比如在wd配置文件中中写id=“check_it_two”,在java中写成 checki_it ...
- [功能帮助类] C#RandomHelper随机数,随机字符,可限制范围-帮助类 (转载)
点击下载 RandomHelper.rar 主要功能如下 .生成一个指定范围的随机整数,该随机数范围包括最小值,但不包括最大值 .生成一个0.0到1.0的随机小数 .对一个数组进行随机排序 . 一:随 ...
- Animating Layout Changes(展开收起)
原文地址:https://developer.android.com/training/animation/layout.html#add (1)设置布局文件: <LinearLayout an ...
- “jni.h”: No such file or directory
VS2010解决方案: 进入 “包含目录“ 方式: 右键项目属性页-> 配置属性->VC++目录->包含目录 在”包含目录“中编辑 添加以下路径: C:\Program Files\ ...
- SQL求差集
数据库环境:SQL SERVER 2008R2 Sql Server有提供求集合差集的函数——EXCEPT.先看看EXCEPT的用法, { <query_specification> | ...
- Oracel JDBC URL 和 Driver 的获取
Driver 的获取 Driver Name: oracle.jdbc.driver.OracleDriver Oracel JDBC URL的获取: URL: jdbc:oracle:thi ...
- 禁用UITextField复制粘贴等方法
要实现此功能只需创建一个继承自UITextField的子类,重写以下方法即可. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{ ...
- Node之express
Express 是一个简洁.灵活的 node.js Web 应用开发框架, 它提供一系列强大的特性,帮助你创建各种 Web 和移动设备应用. 如何安装: npm install -g express ...