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. TOSCA自动化测试工具--TestSuite Components

    TestSuite Components 分了6块

  2. 20145216史婧瑶《Java程序设计》第三次实验报告

    实验三 敏捷开发与XP实践 实验内容 使用git上传代码,两个人进行小组合作,队友下载代码并修改再重新上传. 实验步骤 一. 使用git上传代码 1.找到需要push的文件所在文件夹,右键点击Git ...

  3. 剑指Offer——最长不包含重复字符的子字符串

    Solution 动态规划. f(i)表示包含第i个字符的最长子串. 如果第i个字符没在之前出现过,那么f(i) = f(i - 1) + 1 如果第i个字符在之前出现过,这个时候应该分两种情况,假设 ...

  4. Python学习札记(五) Basic2 字符串和编码

    参考:字符串和编码 Note A.字符编码 1.计算机处理文本 == 将文本转换为二进制 => 处理 2.8 bit(比特) = 1 byte(字节) 一个字节所能表示的最大的十进制整数是255 ...

  5. appium 处理滑动的方法

    appium 处理滑动的方法是 swipe(int start-x, int start-y, int end-x, int end-y, int during) - Method in class ...

  6. 好的Mysql 查询语句

    select swr.id,swr.name,swr.sort as type,count(swl.id) as nums,ifnull(sum(swl.package_num),0) package ...

  7. BZOJ3224 Tyvj 1728 普通平衡树(Treap)

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. Linux安装ipvsadm

    一.介绍 ipvs称之为IP虚拟服务器(IP Virtual Server,简写为IPVS).是运行在LVS下的提供负载平衡功能的一种技术 二.安装 1.下载 http://www.linuxvirt ...

  9. 代码审查工具Sonarqube安装

    前言:在项目开发当中,完成需求并上线是一件很开心的事情,但为了能按时上线功能不得不为了完成功能而写代码,写的时候觉得先把功能上了以后再回头优化此处代码,但真正上线之后你就会发现你再也不想去修改之前遗留 ...

  10. 【小而优】 如何实现 tail -f 动态显示日志时高亮显示关键字

    前言 如果你在linux下工作,那用tail -f跟踪一个日志文件的输出内容应该是家常便饭了. 但是,有时你更关心的是一些敏感字词,希望能够在动态跟踪的同时,把这些字词高亮出来,比如日志中的 ERRO ...