Reorder List [LeetCode]
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}
.
Solution: make sure the last element points to NULL in the new list;
void reorderList(ListNode *head) {
vector<ListNode * > nodes;
ListNode * current = head;
while(current != NULL) {
nodes.push_back(current);
current = current->next;
}
for(int i = ; i < nodes.size() / ; i ++){
int next = nodes.size() - - i;
if(i < next){
ListNode * tmp = nodes[i]->next;
nodes[i]->next = nodes[next];
nodes[next]->next = tmp;
}
}
if(nodes.size() > )
nodes[nodes.size() / ]->next = NULL;
}
void reorderList(ListNode *head) {
vector<ListNode * > nodes;
ListNode * current = head;
while(current != NULL) {
nodes.push_back(current);
current = current->next;
}
for(int i = ; i < nodes.size() / ; i ++){
if(i < nodes.size() - - i){
nodes[i]->next = nodes[nodes.size() - - i];
if(i + < nodes.size() - - i)
nodes[nodes.size() - - i]->next = nodes[i + ];
else
nodes[nodes.size() - - i]->next = NULL;
}
}
if(nodes.size() % == )
nodes[nodes.size() / ]->next = NULL;
}
Reorder List [LeetCode]的更多相关文章
- Reorder List leetcode java
题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must d ...
- 143. Reorder List - LeetCode
Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...
- Reorder List [leetcode] 这两种思路
第一个想法随着vector保存全部Node* 表拼接出来 void reorderList(ListNode *head) { vector<ListNode*> content; Lis ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- [LeetCode] 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 th ...
- 【Leetcode】Reorder List JAVA
一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must ...
- 【Leetcode】143. Reorder List
Question: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You ...
随机推荐
- Vmware10.0 安装系统以及使用笔记
1.安装教程参考 大致分为:vmware10.0安装-------建立虚拟机---------设置虚拟机---------启动虚拟机(IOS安装)---------安装系统---------安装vmt ...
- C# 文件流相关操作
二进制转换成图片: MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(ms ...
- 高效的使用STL
高效的使用STL 仅仅是个选择的问题,都是STL,可能写出来的效率相差几倍: 熟悉以下条款,高效的使用STL: 当对象很大时,建立指针的容器而不是对象的容器 1)STL基于拷贝的方式的来工作,任何需要 ...
- JQery 中的 $(".bb:eq(1)") eq () 解释。。
这是一段代码: <div id="bb">a</div> <div id="bb">b</div> <di ...
- 使用Select命令创建菜单
创建文本菜单的一半功夫都花在了创建菜单布局和获取输入的字符上.bash shell提供了一个很容易上手的小工具来自动完成这些工作select命令允许从单个命令行创建菜单,然后在提取输入的答案并自动处理 ...
- c++操作io常见命令
用c++练习下 系统常见io命令. 1)显示文档的文本 2)统计文本单词数字 3)列出目录所有文件 ,递归思路 4)查找第一个匹配的字符. 5)文本单词排序, 快速排序,其实还是递归思路 6)文本单词 ...
- JS 命名冲突
1. JS中全局变量和局部变量重名会导致在指定域内无法取到变量: 2. 取出的结果为undefined;
- python 标准库
https://www.zhihu.com/question/24590883 https://www.zhihu.com/question/20501628 http://blog.csdn.net ...
- eclipse xml 注释快捷键
注释:CTRL + SHIFT + / 撤销注释:CTRL + SHIFT + \
- golang操作文件的四种方法
golang追加内容到文件末尾 字数349 阅读54 评论0 喜欢2 golang读写文件,网上很多教程了但是今天有个需求,想要把内容追加写到文件末尾google了好久,没有查到研究了一会儿file库 ...