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→…
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(重序链表)的更多相关文章
- [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 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 【编程题目】请修改 append 函数,利用这个函数实现两个非降序链表的并集
42.请修改 append 函数,利用这个函数实现(链表):两个非降序链表的并集,1->2->3 和 2->3->5 并为 1->2->3->5另外只能输出结 ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 剑指Offer14 逆序链表
/************************************************************************* > File Name: 14_Revers ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- 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 ...
随机推荐
- spark[源码]-sparkContext详解[一]
spark简述 sparkContext在Spark应用程序的执行过程中起着主导作用,它负责与程序和spark集群进行交互,包括申请集群资源.创建RDD.accumulators及广播变量等.spar ...
- message from server: "Host '192.168.6.68' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts
系统或者程序连接数据报错 null, message from server: "Host '192.168.6.68' is blocked because of many connect ...
- 重定向符号和tee命令区别
来源:http://zyp88.blog.51cto.com/1481591/1604036 功能说明:读取标准输入的数据,并将其内容输出成文件. 语 法:tee [-ai][--help][-- ...
- CSS Dimension(尺寸)
CSS Dimension(尺寸) 一.简介 CSS 尺寸 (Dimension) 属性允许你控制元素的高度和宽度.同样,它允许你增加行间距. 二.Dimension(尺寸)属性值 三.示例 (1)示 ...
- encoder-decoder环境部署问题
pip -v 2.7 cp -r pip2.7 pip pip list appdirs (1.4.3)cycler (0.10.0)distribute (0.7.3)extern (0.1. ...
- pxe基于虚拟机的自启动
环境系统:centos6.4 min版 虚拟机实现:提供的服务器ip为192.168.0.105,桥接 安装dhcp服务: yum -y install dhcp 配置dhcp服务,使能够成功启动: ...
- 20145327 《Java程序设计》第八周学习总结
20145327 <Java程序设计>第八周学习总结 教材学习内容总结 NIO使用频道(channel)来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区(Buffer)容量,在缓冲区 ...
- C++求矩阵的鞍点
矩阵的鞍点就是指它在本行中的值最大,在本列中的值最小. 求解思路: 求出每行的最大值MaxRow以及每列的最小值MinColumn 保存行最大值的位置和列最小值的位置 如果行最大值得位置和列最小值的相 ...
- Socket 是嘛玩意儿(简单聊聊)
网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 建立网络通信连接至少要一对端口号(socket).socket本质是编程接口(API),对TCP/IP的封装 ...
- 推荐一个快速了解移植uboot以及linux到新板子上的ppt教程
链接地址在此: https://elinux.org/images/2/2a/Schulz-how-to-support-new-board-u-boot-linux.pdf