62. 链表重排[Reorder List]
【本文链接】
http://www.cnblogs.com/hellogiser/p/reorder-list.html
【题目】
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,5,6,7}, reorder it to {1,7,2,6,3,5,4}.
【分析】
题目思路比较直接:
(1)找到链表的中间节点,把链表划分成2个子链表;如果原链表长度为奇数,那么第一个子链表的长度多1;
(2)翻转第二个子链表;
(3)交叉合并两个子链表。
例如{1,2,3,4,5,6,7}
(1)找到链表的中间节点为4,把链表划分成2个子链表:{1,2,3,4}和{5,6,7};
(2)翻转第二个子链表得到{7,6,5}
(3)交叉合并{1,2,3,4}和{7,6,5}得到{1,7,2,6,3,5,4}
【代码】
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
// 62_ReorderList.cpp : Defines the entry point for the console application.
// /* version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/5/30 */ #include "stdafx.h" struct ListNode // find middle node of list // reverse list // cross merge list ListNode *node1 = head1, *node2 = head2; // reorder list // find middle node of list |
【参考】
http://blog.csdn.net/whuwangyi/article/details/14146461
【本文链接】
http://www.cnblogs.com/hellogiser/p/reorder-list.html
最新文章
- hdfs以及hbase动态增加和删除节点
- Mysql数据库 - 增删改
- WebSphere MQ 安装
- SNS网站成功原因剖析_完结
- 开启software protection报错
- magic_quotes_gpc 、 magic_quotes_runtime 、 magic_quotes_sybase 介绍
- [Android] 关于系统工具栏和全屏沉浸模式
- DWZ(JUI) 教程 中如何整合第三方jQuery插件
- linux 2.6 互斥锁的实现-源码分析
- 关于JFace带复选框的树
- UISearchController的使用。(iOS8+)
- 听同事讲 Bayesian statistics: Part 2 - Bayesian inference
- js框架页跳转
- 什么是Bash Shell的内建(build in)命令
- 解析好的静态页面.shtml浏览器无法解析.需要apache解析后再返回给浏览器
- 4)C语言指针(C自考学习)
- Standard PHP Library(SPL)中的数据结构
- Vuex 存储||获取后台接口数据
- linux架构师之路!
- 早期自学jQuery-二事件
热门文章
- OC基础--description方法
- Tomcat_修改代码后tomcat是否需要重启
- Java基础-继承-子类与父类执行顺序
- Learn sed using these command on Linux(流线式编辑器——sed)
- 42.Android之ListView中ArrayAdapter简单学习
- 【codevs1409】 拦截导弹 2
- windows进程/线程创建过程 --- windows操作系统学习
- html,body { margin:0; padding:0;border:0}
- Java线程之间通信
- 改变了Tomcat路径后无法卸载和重装的解决办法