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 ...
随机推荐
- go学习资料
go书单 1.代码规范 https://github.com/golang/go/wiki/CodeReviewComments 2.基础知识 先看: https://github.com/mikel ...
- Debug与Trace工具类的应用
在写Console程序的时候,能够使用Console.WriteLine()来时时的输出程序的执行状态和各种參数此刻的信息.可是假设是Windows Form程序,我们要怎样实时的观測程序的执行状况呢 ...
- Python内置函数之sorted()
sorted(iterable,*,key=None,reverse=False) 对可迭代对象进行排序,默认ASCII进行排序. 例子: sorted(iterable,*,key=None,rev ...
- [Java]事件驱动程序设计
事件驱动模型三大要素 1)事件源:能接收外部事件的源体: 2)监听器xListener:能接收事件源通知的对象: 3)处理器Handler:用于处理事件的对象. 在Java中使用监听器对象处理事件的方 ...
- Hadoop2.6.0版本MapReudce示例之WordCount(一)
一.准备测试数据 1.在本地Linux系统/var/lib/Hadoop-hdfs/file/路径下准备两个文件file1.txt和file2.txt,文件列表及各自内容如下图所示: 2.在hdfs中 ...
- postman 测试API - 获取/调用 token
1.使用全局变量保存token 2.再调用 参考文章 http://www.jianshu.com/p/13c8017bb5c8 https://testerhome.com/topics/6555
- diamond源码阅读-目录监控
PathNode(Path)StandardWatchEventKind(WatchEvent)Watchable(WatchKey WatchService WatchEvent)WatchKey( ...
- 写shader注意的一些报错
1.Shader warning in 'Custom/1': Both vertex and fragment programs must be present in a CGPROGRAM. Ex ...
- APNS的推送机制 3
APNS的推送机制 首先我们看一下苹果官方给出的对iOS推送机制的解释.如下图 Provider就是我们自己程序的后台服务器,APNS是Apple Push Notification Service的 ...
- 圆环自带动画进度条ColorfulRingProgressView
这是项目中遇到了,我也是借鉴大神的, 下载地址:https://github.com/oooohuhu/ColorfulRingProgressView 我把它导入了github中了,里面有详细的使用 ...