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 ...
随机推荐
- C#学习笔记-----C#枚举中的位运算权限分配
一.基础知识 什么是位运算? 用二进制来计算,1&2:这就是位运算,其实它是将0001与0010做位预算 得到的结果是 0011,也就是3 2.位预算有多少种?(我们就将几种我们权限中会 ...
- Shell编程基础教程6--shell函数
6.shell函数 6.1.定义函数 简介: shell允许将一组命令集或语句形成一个可用块,这些块成为shell函数 定义函数的格式 ...
- SOLR+LUCENE错误
java.lang.NoClassDefFoundError: org/apache/lucene/analysis/synonym/SynonymFilter 该错误发生在自定义SOLR服务器时,原 ...
- jqueryEasyUI:tabs扩展:给tabs组件绑定双击事件 分类: JqueryEasyUI 2014-09-29 14:36 537人阅读 评论(0) 收藏
实现代码: $.extend($.fn.tabs.methods, { /** * 绑定双击事件 * @param {Object} jq * @param {Object} caller 绑定的事件 ...
- 数据结构之图 Part1
Part 1 预计使用7天的时间来过掉图相关的数据结构.第一天主要是一天图的基本概念,熟练掌握定义是一切交流和沟通的基础. 1定义 1.1图 有穷非空顶点,外加边. G(V,E) Graph Vert ...
- C#交互功能的演化
(此文章同时发表在本人微信公众号“dotNET每日精华文章”,欢迎右边二维码来关注.) 题记:Miguel de Icaza在最近发表的一篇博文中畅谈了Mono及其相关产品中的C#交互特性的演化情况. ...
- Flash Media Server 4.0 破解 注册
Adobe Flash Media Interactive Server 3.5s/n:1373-5047-2985-0514-5175-0098 s/n: 1373-5632-4666-9521-8 ...
- 用C#基于WCF创建TCP的Service供Client端调用
本文将详细讲解用C#基于WCF创建TCP的Service供Client端调用的详细过程 1):首先创建一个Windows Service的工程 2):生成的代码工程结构如下所示 3):我们将Servi ...
- 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis
1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...
- loadrunner处理HTTP重定向请求
//place this in global.h int HttpRetCode; int i=0; char depthVal[10]; char cTransactName[2000 ...