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. jquery相对选择器,又叫context选择器,上下文选择器;find()与children()区别

    jquery相对选择器有两个参数,jQuery函数的第二个参数可以指定DOM元素的搜索范围(即以第二个参数指定的内容为容器查找指定元素). 第二个参数的不同的类型,对应的用法如下表所示. 类型 用法 ...

  2. css选择器选择顺序是从右往左的,为什么?

    https://segmentfault.com/q/1010000000713509 为什么 CSS 选择器解析的时候是从右往左? CSS 的后代选择器本身就是一种在标准里面不那么推荐的方式. 首先 ...

  3. linux 中的快捷键

    终端快捷键 tab=补全 ctrl+a=开始位置 ctrl+e=最后位置 ctrl+k=删除此处至末尾所有内容 ctrl+u=删除此处至开始所有内容 ctrl+d=删除当前字母 ctrl+w=删除此处 ...

  4. poj 1106 Transmitters (叉乘的应用)

    http://poj.org/problem?id=1106 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4488   A ...

  5. SQL Server 中 RAISERROR 的用法

    From : http://www.cnblogs.com/xugang/archive/2011/04/09/2010216.html   raiserror  是由单词 raise error 组 ...

  6. 一种json生成html的思路

    输入: [{ tag:"ul", attribute:{ class:"father6" }, property:{ className:"fathe ...

  7. ubuntu下配置hosts

    由于Chrome浏览器访问问题,需要配置hosts. 在Ubuntu系统下,需要修改/etc/hosts文件,修改完之后要重启网络.具体过程如下:1.修改hostssudo vi /etc/hosts ...

  8. 管道寄售库存MRKO结算后,冲销问题

    管道寄售库存MRKO结算后,冲销问题 1.通常使用MIRO对采购订单进行发票校验后,若发现校验错误,直接使用MR8M取消发票校验,同时手工F-03对借发票校验借方GRIR和取消发票校验的贷方GRIR进 ...

  9. poj1434Fill the Cisterns!(二分)

    链接 题目说给你n个水箱,初始是没有水的,每个的高低位置可能不同,给了你初始的水箱底部所处的水平线位置,问给你V体积水时,水的水平线位置. 直接二分位置p,对于每一个底部低于水平线位置的水箱,里面的水 ...

  10. WCF入门教程(vs2010)

    这几天挺别人提起WCF,鄙人之前只知道WPF,对WCF这个东东不甚了解,经过查阅网上的资料略有所得,和大家交流一下. 首先WCF是什么? Windows Communication Foundatio ...