Reorder List

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→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的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

  5. LeetCode解题报告—— Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结

    前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...

随机推荐

  1. SOAP消息的传递

    上一篇说了SOAP消息的创建,那么创建好了的SOAP消息要怎么发送给服务端呢? public class SoapTest { private String wsdlUri = "http: ...

  2. CGI初识

    ---恢复内容开始--- 转自http://www.moon-soft.com/program/bbs/readelite887957.htm 用 C/C++ 写 CGI 程序 小传(zhcharle ...

  3. ios中创建可以拖动的view原理和实现详解

    有时候我们会需要在界面上拖动view;uiview是继承于uiresponder的,所以可以响应触摸相关的事件. 重点是以下一组方法: - (void)touchesBegan:(NSSet *)to ...

  4. Spring 注解回顾

    [copy] 参考资料 赵蒙

  5. BI任务列表

    了解点击流系统和pv/uv的相关计算 关于pv的那些事!! ···············································2014-09-10 homework做了些什 ...

  6. Ubuntu12.04中安装ns-allinone-2.34

    1.首先安装ns2所需的组件.库之类: $sudo apt-get update $sudo apt-get install build-essential $ tcl8.-dev tk8. tk8. ...

  7. 利用case when 减少表扫描次数

    数据库环境:SQL SERVER 2008R2 有网友希望有人帮他优化一下他的SQL,SQL语句如下: WITH T AS ( SELECT B.O_Money MON,B.O_States STAT ...

  8. 优化有标量子查询的SQL

    数据库环境:SQL SERVER 2008R2 今天在数据库中抓出一条比较耗费资源的SQL,只返回904条数据,居然跑了40多分钟.SQL及对应的数据量如下图: SELECT saft04.cur_y ...

  9. HDU_1406 完数

    Problem Description 完数的定义:如果一个大于1的正整数的所有因子之和等于它的本身,则称这个数是完数,比如6,28都是完数:6=1+2+3:28=1+2+4+7+14. 本题的任务是 ...

  10. python中的“引用”和C++的引用

    python并不刻意区分“按值传递”和“按引用传递”. 在底层,python将值分为不可变对象(比如int,str)和可变对象(比如列表).所有的变量都是对某个对象的引用,赋值(=)和函数参数传递,都 ...