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 ...
随机推荐
- Java并发包中线程池的种类和特点介绍
Java并发包提供了包括原子量.并发集合.同步器.可重入锁.线程池等强大工具这里学习一下线程池的种类和特性介绍. 如果每项任务都分配一个线程,当任务特别多的时候,可能会超出系统承载能力.而且线程的创建 ...
- Java Calendar类总结
在实际项目当中,我们经常会涉及到对时间的处理,例如登陆网站,我们会看到网站首页显示XXX,欢迎您!今天是XXXX年....某些网站会记录下用户登陆的时间,比如银行的一些网站,对于这些经常需要处理的问题 ...
- 枚举转SelectList扩展方法
public enum Avbc { Red=1, Blue=2, Whilt=3, Black=4 } public st ...
- 20145240《网络对抗》Web基础
Web基础 实验后回答问题 什么是表单? 表单在网页中主要负责数据采集功能. 一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器的方法. 表单域 ...
- Error: Flash Download failed - "Cortex-M0"
今天在调试程序时.DEMO板下载几次后就提示Flash下载失败: Keil v4里面的设置都没有动过.不可能被修改.此时.使用新唐的ICP Progra ...
- 解读:hadoop压缩格式
Hadoop中用得比较多的4种压缩格式:lzo,gzip,snappy,bzip2.它们的优缺点和应用场景如下: 1). gzip压缩 优点:压缩率比较高,而且压缩/解压速度也比较快:hadoop本身 ...
- 配置linux内核输出所有的log信息
答案:修改内核文件include/linux/printk.h (内核版本为4.9) 修改前 #define CONSOLE_LOGLEVEL_DEFAULT 7 /* anything MORE ...
- [参考]C的scanf 和 C++的fscanf 的用法
说明:本文不适合新手学习,适合用来做参考.本文参考有其他博客的内容,不过年代久远已经忘记了,在此感谢各位博主! scanf函数 用 法:int scanf(char *format[,argument ...
- 05_MySQL常见函数_分组函数
# 分组函数/*功能: 统计,又称为聚合函数,统计函数,组函数 传入一组值,统计后得到一个值 分类: sum 求和,avg 平均值,max 最大值,min 最小值,count 计算个数 特点: 1. ...
- springboot idea 配置热加载
在idea 配置springboot的热加载,只需要三步: 第一步.引用jar包 <dependency> <groupId>org.springframework.boot& ...