Data Structure Linked List: Flattening a Linked List
http://www.geeksforgeeks.org/flattening-a-linked-list/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
#include <set>
using namespace std; struct node {
int data;
node *right;
node *down;
node(int d = ) : data(d), right(NULL), down(NULL) { }
}; struct cmp {
bool operator() (node *a, node *b) {
return a->data > b->data;
}
}; void push(node* &head, int k) {
node *new_node = new node(k);
new_node->down = head;
head = new_node;
} node* flatten(node *&root) {
priority_queue<node*, vector<node*>, cmp> S;
while (root) {
S.push(root);
root = root->right;
}
root = NULL;
node *p;
while (!S.empty()) {
node *top = S.top();
S.pop();
if (!root) {
root = top;
p = top;
}
else {
p->right = top;
p = p->right;
}
if (top->down) S.push(top->down);
}
return root;
} void print(node *head) {
while (head) {
cout << head->data << " ";
head = head->right;
}
} int main() {
node *root = NULL;
push( root, );
push( root, );
push( root, );
push( root, ); push( ( root->right ), );
push( ( root->right ), ); push( ( root->right->right ), );
push( ( root->right->right ), );
push( ( root->right->right ), ); push( ( root->right->right->right ), );
push( ( root->right->right->right ), );
push( ( root->right->right->right ), );
push( ( root->right->right->right ), ); root = flatten(root);
print(root);
return ;
}
Data Structure Linked List: Flattening a Linked List的更多相关文章
- [转]Data Structure Recovery using PIN and PyGraphviz
Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...
- 面试总结之数据结构(Data Structure)
常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- Finger Trees: A Simple General-purpose Data Structure
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...
- Mesh Data Structure in OpenCascade
Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...
- ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
随机推荐
- mongo: 索引
索引创建 在学习索引之前,我们先看一下,如果没有添加索引时,我们用explain()函数,查看查询计划是什么样的. 发现使用的是BasicCursor,那么就代表我们没有索引,当我们查某一个数据的时候 ...
- MySQL 数据库备份种类以及经常使用备份工具汇总
mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) mysql> show master st ...
- Quartus和ISE调用Synplify进行综合的问题
分别尝试采用Quartus和ISE调用第三方综合软件Synplify进行综合. [软件版本] Quartus II 13.0 (SP).ISE 14.4 .Synplify 201303. [问题描述 ...
- Spark高速上手之交互式分析
1.1 Spark交互式分析 执行Spark脚本前,启动Hadoop的HDFS和YARN.Spark的shell提供 了简单方式去识别API.相同也有一个强大的工具去交互式地分析数据. 两种语言有这 ...
- applicationContext-XXX.xml和XXX-servlet.xml的区别
1.ApplicationContext.xml 是spring 全局配置文件,用来控制spring 特性的 2.dispatcher-servlet.xml 是spring mvc里面的,控制器. ...
- C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 1
Chapter 1.1 1. 每个C++程序都必须有且只能有一个main函数,main函数的返回类型必须是int.操作系统通过调用main函数来运行C++程序. 2. 一个函数的定义包含四部分:返回类 ...
- [转]Ubuntu Server命令行更换软件源
sucd /etc/aptwget http://mirrors.163.com/.help/sources.list.lucidmv sources.list sources.list.backup ...
- PorterDuffXferMode不对的真正原因PorterDuffXferMode深入试验)
菜鸡wing遇敌PorterDuffXferMode,不料过于轻敌,应战吃力. 随后与其大战三天三夜.三百余回合不分胜负. 幸得 @咪咪控 相助,侥幸获胜. keyword:PorterDuffXfe ...
- 详解path和classpath的区别
详解path和classpath的区别 path的作用 path是系统用来指定可执行文件的完整路径,即使不在path中设置JDK的路径也可执行JAVA文件,但必须把完整的路径写出来,如C:\Progr ...
- 11 Memcached 缓存雪崩现象
一:Memcached 缓存雪崩现象(1)造成原因:一般是由某个节点失效,导致其他的节点的缓存命中率下降,缓存中缺失的数据查询,短时间内造成的数据库服务器奔溃.重启DB,短期内又被压垮,但缓存的数据增 ...