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

(1)找出中间节点

(2)将后部分节点反转

(3)将前部分节点和后部分节点合并

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <list> using namespace std; struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL){}
}; void printList(ListNode* head){
while(head!=NULL){
cout<<"->"<<head->val;
head = head->next;
}
cout<<endl;
} ListNode* reverse(ListNode *head){
ListNode *pre = head;
ListNode *p = head->next;
head->next =NULL;
while(p!=NULL){
ListNode* tmp = p->next;
p->next = pre;
pre = p;
p = tmp;
}
return pre;
} ListNode* merge(ListNode *head1, ListNode *head2){
ListNode* p = head1;
while(head2!=NULL){
ListNode *tmp = head2->next;
head2->next = p->next;
p->next = head2;
p = p->next->next;
head2 = tmp;
}
return head1; } void reorderList(ListNode *head){
if(head == NULL || head->next == NULL) return;
ListNode *slow = head,*fast = head;
while(fast->next && fast->next->next){
slow = slow->next;
fast = fast->next->next;
}
ListNode *head2 = slow->next;
slow->next = NULL;
ListNode *head1 = head;
//printList(head2);
head2 = reverse(head2);
//printList(head2);
head = merge(head1,head2);
} int main(){
ListNode *head= new ListNode();
ListNode *p = head;
for(int i =; i <= ; ++ i){
ListNode *a= new ListNode(i);
p->next = a;
p = a;
}
reorderList(head);
printList(head);
}

Leetcode ReorderList的更多相关文章

  1. leetcode — reorder-list

    /** * Source : https://oj.leetcode.com/problems/reorder-list/ * * Given a singly linked list L: L0→L ...

  2. reorder-list leetcode C++

    Given a singly linked list L: L 0→L 1→-→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→- You ...

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

  4. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  5. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  6. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  7. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  8. 【LEETCODE OJ】Reorder List

    Problem link: http://oj.leetcode.com/problems/reorder-list/ I think this problem should be a difficu ...

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

随机推荐

  1. 早上3:30左右起来发现时候电脑在一致叫唤就是一个usb的接口可能是鼠标

    然后看了下也没有网络了,早上起来就打了一个电话给网管,就开通了.是没有及时开通.

  2. logstash json和rubydebug 第次重启logstash都会把所有的日志读完 而不是只读入新输入的内容

    查看一下agent端的shipper的配置: # cat logstash_test2.shipper.conf input { file { path => ["/apps/logs ...

  3. @ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4 "/" $NF}'中"[ :]+" 是什么意思?@

    http://blog.csdn.net/zhuying_linux/article/details/6822987

  4. 手机站点动态效果插件TouchSlide

    今天看到TouchSlide插件,觉得非常不错,关于使用情况请看demo,下载地址:http://www.superslide2.com/TouchSlide/downLoad.html

  5. python面向对象进阶 反射 单例模式 以及python实现类似java接口功能

    本篇将详细介绍Python 类的成员.成员修饰符.类的特殊成员. 类的成员 类的成员可以分为三大类:字段.方法和特性. 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存 ...

  6. tkprof工具详解二(一些实例)

    TKPROF是一个可执行文件,自带在Oracle Server软件中,无需额外的安装. 该工具文件可以用来解析ORACLE的SQL TRACE(10046) 以便生成更可读的内容.  实际上tkpro ...

  7. RTP timestamp与帧率及时钟频率的关系

    转自:http://blog.csdn.net/jasonhwang/article/details/7316128 RTP timestamp是用时钟频率(clock rate)计算而来表示时间的. ...

  8. AxureRP7.0各类交互效果汇总帖(转)

    了便于大家参考,我把这段时间发布分享的所有关于AxureRP7.0的原型做了整理. 以下资源均有对应的RP源文件可以下载. 当然 ,其中有部分是需要通过完成解密游戏[攻略]才能得到下载地址或者下载密码 ...

  9. PRD产品需求文档

    什么是PRD? PRD是Product Requirement Document的英文缩写,即产品需求文档的意思.PRD昰产品流程中的最后一步工作,是将原型中的功能.界面具象化描述,是提交给设计(UI ...

  10. C++读取文件夹中所有的文件或者是特定后缀的文件

    由于经常有读取一个文件夹中的很多随机编号的文件,很多时候需要读取某些特定格式的所有文件. 下面的代码可以读取指定文件家中的所有文件和文件夹中格式为jpg的文件 参考: http://www.2cto. ...