Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→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]的更多相关文章

  1. 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 ...

  2. 143. Reorder List - LeetCode

    Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...

  3. Reorder List [leetcode] 这两种思路

    第一个想法随着vector保存全部Node* 表拼接出来 void reorderList(ListNode *head) { vector<ListNode*> content; Lis ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  6. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

  7. [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 ...

  8. 【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 ...

  9. 【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 ...

随机推荐

  1. Cheatsheet: 2013 12.01 ~ 12.16

    Mobile Performance Tuning On Android Interoperation Issues in Mixed C/C++/Objective-C Development We ...

  2. pupper基线加固

    1.  概述 puppet是一个开源的软件自动化配置和部署工具,它使用简单且功能强大,正得到了越来越多地关注,现在很多大型IT公司均在使用puppet对集群中的软件进行管理和部署,如google利用p ...

  3. SQL语句最基本的性能优化方法

    有些人还不知道sql语句的基本性能优化方法,在此我简单提醒一下,最基本的优化方法:   1.检查是否缺少索引.调试的时候开启“包括实际的执行计划”   执行后会显示缺少的索引,   然后让dba帮助添 ...

  4. ABAP锁、数据库锁

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. FLASH CC 2015 CANVAS (二)html中写JS调用flash中的元件、函数、变量

    注意 此贴 为个人边“开荒”边写,所以不保证就是最佳做法,也难免有错误! 正式教程会在后续开始更新 当你导出第一个canvas后,你会在保存fla的文件夹里 (每个项目默认位置)看到 如下文件,(请先 ...

  6. Doragon Kuesuto 1.6

    /* * <<D Q>> * * Author xkfx<wyzxk_fx@163.com> * * 游戏规则:利用适当的决策,在13回合内击杀恶龙取得胜利. * ...

  7. 手动配置WCF宿主的.config文件遇到的几种错误

    今天尝试用控制台应用程序作为WCF宿主,遇到几个问题,这几个问题虽然都不难,但寻找解决方案相当费时费力,做记录. WCF标准的配置文件为: <system.serviceModel>    ...

  8. spring.hibernate设置参数的位置问题

    1.spring的注解设置在set方法上 2.hibernate的注解设置在get方法上

  9. 面向对象的static关键字(类中的static关键字)

    转自:http://blog.csdn.net/xiayefanxing/article/details/7382192 http://www.cnblogs.com/SelaSelah/archiv ...

  10. JDK中的Timer和TimerTask详解(zhuan)

    http://www.cnblogs.com/lingiu/p/3782813.html ************************************************** 目录结构 ...