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 ...
随机推荐
- (转)C# Winform应用程序占用内存较大解决方法整理
背景: 微软的 .NET FRAMEWORK 现在可谓如火如荼了.但是,.NET 一直所为人诟病的就是“胃口太大”,狂吃内存,虽然微软声称 GC 的功能和智能化都很高,但是内存的回收问题,一直存在困扰 ...
- MyBatis Mapper 接口如何通过JDK动态代理来包装SqlSession 源码分析
我们以往使用ibatis或者mybatis 都是以这种方式调用XML当中定义的CRUD标签来执行SQL 比如这样 <?xml version="1.0" encoding=& ...
- [C和指针]第四部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CUBRID学习笔记 43 insert into
cubrid的中sql查询语法insert into ------ 官方文档是英文的,看不明白可以参看ocracle的同类函数说明.很多都是一样的. INSERT INTO a_tbl1(id) VA ...
- 堆内存指针的管理类(禁,引数(指针copy),值copy,移)
//copyp template<typename T> class pm_copyP{ public: pm_copyP(T* _p); pm_copyP(const pm_copyP& ...
- org.apache.jasper.JasperException: Expecting "jsp:param" standard action with "name" and "value" attributes
jasper 英 ['dʒæspə] 美 ['dʒæspɚ] 跟读 口语练习 n. 碧玉:墨绿色 n. (Jasper)人名:(德)雅斯佩尔:(西)哈斯佩尔 JasperException 异 ...
- nodejs学习笔记<四>处理请求参数
在web开发中处理请求参数是个非常常见的工作:nodejs提供了了querystring用来处理请求参数. querystring常用方法有:parse,stringify. (1)parse: 解析 ...
- poj1584A Round Peg in a Ground Hole
链接 题意甚是难懂!这是第二遍做这道题了,依旧无法理解题意,搜了下题意... 首先需要判断是不是为凸多边形.(从一个顶点走一遍即可,要注意顺逆时针,题目中没有指明) 其次看一下圆是不是能够放入多边形内 ...
- Struts1 标签库 说明
Struts提供了五个标签库,即:HTML.Bean.Logic.Template和Nested. HTML标签 : 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单 ...
- gcc, numpy, rabbitmq等安装升级总结
1. 公司在下面目录安装了gcc-4.8.2,以支持c++11,可以通过在bashrc中添加来实现: PATH=/opt/compiler/gcc-4.8.2/bin:$PATH 2. 公司环境切换到 ...