Leetcode ReorderList
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}.
(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的更多相关文章
- leetcode — reorder-list
		/** * Source : https://oj.leetcode.com/problems/reorder-list/ * * Given a singly linked list L: L0→L ... 
- 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 ... 
- [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算法分类
		利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ... 
- leetcode bugfree note
		463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ... 
- LeetCode题目分类
		利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ... 
- LeetCode OJ 题解
		博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ... 
- 【LEETCODE OJ】Reorder List
		Problem link: http://oj.leetcode.com/problems/reorder-list/ I think this problem should be a difficu ... 
- 【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 ... 
随机推荐
- phpcms 标签
			都说pc标签{pc:content参数名="参数值"参数名="参数值"参数名="参数值"} 但是 参数名对应的具体参数值有那些,菜鸟就不知道 ... 
- CLR via C#学习笔记----知识总概括
			第1章 CLR的执行模型 托管模块的各个组成部分:PE32或PE32+头,CLR头,元数据,IL(中间语言)代码. 高级语言通常只公开了CLR的所有功能的一个子集.然而,IL汇编语言允许开发人员访问C ... 
- webrtc中的带宽自适应算法
			转自:http://www.xuebuyuan.com/1248366.html webrtc中的带宽自适应算法分为两种: 1, 发端带宽控制, 原理是由rtcp中的丢包统计来动态的增加或减少带宽,在 ... 
- 轻松学习RSA加密算法原理
			转自:http://blog.csdn.net/sunmenggmail/article/details/11994013 http://blog.csdn.net/q376420785/articl ... 
- x264中I,P,B帧和PTS,DTS的关系
			转自:http://www.cppblog.com/tx7do/archive/2013/01/30/197633.html 基本概念: I frame :帧内编码帧 又称intra picture, ... 
- php随机生成验证码
			我们经常需要服务器向前端发送验证码,验证码需要随机产生,下面的用简单的代码实现了这一过程: <?php $pool='0123456789abcdefghijklmnopqrstuvwxyzAB ... 
- 如何设计一个优秀的API(转)
			到目前为止,已经负责API接近两年了,这两年中发现现有的API存在的问题越来越多,但很多API一旦发布后就不再能修改了,即时升级和维护是必须的.一旦API发生变化,就可能对相关的调用者带来巨大的代价, ... 
- python web编程-CGI帮助web服务器处理客户端编程
			这几篇博客均来自python核心编程 如果你有任何疑问,欢迎联系我或者仔细查看这本书的地20章 另外推荐下这本书,希望对学习python的同学有所帮助 概念预热 eb客户端通过url请求web服务器里 ... 
- java发展史与java的语言特性
			概述: Java 体系比较庞杂,功能繁多,这也导致很多人在自学 Java 的时候总是感觉无法建立 全面的知识体系, 无法从整体上把握Java 的原因. 在这里我们先简单了解一下Java 的版本. 具体 ... 
- 遍历进程活动链表(ActiveProcessLinks)、DKOM隐藏进程
			1.EPROCESS结构体 EPROCESS块来表示.EPROCESS块中不仅包含了进程相关了很多信息,还有很多指向其他相关结构数据结构的指针.例如每一个进程里面都至少有一个ETHREAD块表示的线程 ... 
