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 ...
随机推荐
- 【转】Intellij IDEA常用配置详解
1. IDEA内存优化 先看看你机器本身的配置而配置. \IntelliJ IDEA 8\bin\idea.exe.vmoptions -------------------------------- ...
- Ffmpeg 探测输入源类型
本文主要记录ffmpeg探测输入源的几个点,方便以后查阅代码.虽然项目时间很赶,还是找个时间做个记录, 好记性不如不如备忘录,时间久了终是会忘记.Ffmpeg版本为4.0.2,版本很重要,Ffmpeg ...
- MQTT--入门 续
1.消息模型: MQTT是一种基于代理的发布/订阅的消息协议.提供一对多的消息分发,解除应用程序耦合.一个发布者可以对应多个订阅者,当发布者发生变化的时候,他可以将消息一一通知给所有的订阅者.这种模 ...
- java中不能用小数点(.)来做分隔符
split()括号里是一个String的参数,所以一定要符合这种:split(".")形式,即点'.'要用双引号""括起来"."在java中 ...
- CentOS4.5下LVS方案
环境描述:本文在配置LVS时使用三台linux,一台做Directorserver (192.168.0.25) ,两台做realserver(192.168.0.127 192.168.0.12,在 ...
- C++中没有定义类的引用。
在有时候由于类太大.须要在类在后面定义: 比如: class Y{ void f(X); }; class X{ //一些成员数据和函数 }; //error 由于c++要求不论什么一个变量在引用之前 ...
- oracle中把函数的执行权限赋个某个用户
赋权:grant execute on function1 to ucr_dtb1;收回执行权限:revoke execute on function1 from ucr_dtb1; 在ucr_dtb ...
- centos关机与重启命令详解
Linux centos关机与重启命令详解与实战 Linux centos重启命令: 1.reboot 普通重启 2.shutdown -r now 立刻重启(root用户使用) 3.shutdo ...
- 怎样查看Eclipse是32位还是64位?
怎样查看Eclipse是32位还是64位? 1.去Eclipse的安装文件夹,找到eclipse.ini 2.打开这个文件.寻找:launcher.library,我的机器上,在第二行 3.查看&qu ...
- CentOS7如何使用U盘安装
前段时间给一台没有光驱的PC安装CentOS7(CentOS-7.0-1406-x86_64-DVD.iso),惯例直接用Universal-USB-Installer直接转换镜像至U盘,顺利启动,却 ...