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

如上例子所示的重序链表问题,先找到中间节点,然后将链表分成两段。将第二段反转后依次插入第一段中就得到了完整的链表,代码如下所示:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
if(!head || !head->next) return;
ListNode * fastNode = head, * slowNode = head;
while(fastNode->next){
fastNode = fastNode->next;
if(fastNode->next){
slowNode = slowNode->next;
fastNode = fastNode->next;
}
}
ListNode * p1 = head;
ListNode * p2 = slowNode->next;
slowNode->next = NULL;//将前一段链表的最后一个节点的下一个赋为值NULL
//将第二个节点指向的链表颠倒过来
ListNode * prev = NULL;
ListNode * curr = p2;
ListNode * tmpNode = NULL;
while(curr){
tmpNode = curr->next;
curr->next = prev;
prev = curr;
curr = tmpNode;
}
p2 = prev;//颠倒之后的首节点
ListNode * tmpNode1, * tmpNode2;
while(p2){
tmpNode1 = p1->next;
p1->next = p2;
tmpNode2 = p2->next;
p2->next = tmpNode1;
p1 = tmpNode1;
p2 = tmpNode2;
}
}
};

代码重复有点多,写的比较乱,见谅见谅。

LeetCode OJ:Reorder List(重序链表)的更多相关文章

  1. [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

    For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...

  2. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  3. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  4. 【编程题目】请修改 append 函数,利用这个函数实现两个非降序链表的并集

    42.请修改 append 函数,利用这个函数实现(链表):两个非降序链表的并集,1->2->3 和 2->3->5 并为 1->2->3->5另外只能输出结 ...

  5. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  6. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  7. 剑指Offer14 逆序链表

    /************************************************************************* > File Name: 14_Revers ...

  8. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  9. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. ThreadLocal的设计理念与作用

    转自:http://www.iteye.com/topic/103804 首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线 ...

  2. yum安装memchache

    转载地址:http://www.cnblogs.com/jiunadianshi/articles/2001334.html 标准的CentOS5软件仓库里面是没有memcache相应的包的,所以,我 ...

  3. 有道云笔记配合MPic+七牛云 自制MarkDown文档图床(适用Typora)

    注:从有道云笔记v6.5开始,有道云笔记会员可以使用MarkDown有道自带的图床.(但是非会员可以采用下面的七牛云图床+MarkDown方法) 0x00 前言 一直用有道云笔记,粘贴图片,做笔记没问 ...

  4. CentOS系统下yum命令的详细使用方法

    yum是什么yum = Yellow dog Updater, Modified 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它能便于管理大量系统的更新问题 yum特点 ...

  5. Jquery编历数组

    <html> <head> <title>编历</title> <script type="text/javascript"& ...

  6. 20145314郑凯杰 《Java程序设计》第2周学习总结 代码开始!

    ---恢复内容开始--- 20145314郑凯杰 <Java程序设计>第2周学习总结 代码开始! 教材学习内容总结 跟着教材的顺序开始总结我学过的内容: 1编辑.编译.运行教材上代码 这部 ...

  7. MysQL使用一高级应用(下)

    连接查询 连接查询分类如下: 表A inner join 表B:表A与表B匹配的行会出现在结果中 表A left join 表B:表A与表B匹配的行会出现在结果中,外加表A中独有的数据,未对应的数据使 ...

  8. jsp判断以某个字母开头

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ tag ...

  9. Java实习二

    链表(java实现) Link.java public class Link{ private Node first; public Link(){ this.first = null; } //判断 ...

  10. C# Memcached 缓存

    之前做的功能,程序可能有不足之处,但还是要记录下 ICacheStrategy.cs文件 public interface ICacheStrategy { /// <summary> / ...