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的更多相关文章

  1. [转]Data Structure Recovery using PIN and PyGraphviz

    Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...

  2. 面试总结之数据结构(Data Structure)

    常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...

  3. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  4. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  5. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  6. 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 ...

  7. Mesh Data Structure in OpenCascade

    Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...

  8. ✡ 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 - ...

  9. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

随机推荐

  1. iOS将数组中的内容分拼接成字符串

    NSString *string = [array componentsJoinedByString:@","];--分隔符

  2. 如何在 Linux 下大量屏蔽恶意 IP 地址

    很多情况下,你可能需要在Linux下屏蔽IP地址.比如,作为一个终端用户,你可能想要免受间谍软件或者IP追踪的困扰.或者当你在运行P2P软件时.你可能想要过滤反P2P活动的网络链接.如果你是一名系统管 ...

  3. HTML5 2D平台游戏开发#5攻击

    目前为止,角色除了基本的移动外还什么都不能做,于是我打算先实现角色的攻击动画.角色的普通攻击一共可以分为三个阶段: 一段斩 二段斩 三段斩 移动攻击 跳跃攻击 触发方式为角色站立时按下J(攻击)键,角 ...

  4. Cannot lock storage /tmp/hadoop-root/dfs/name. The directory is already locked.

    [root@nn01 bin]# ./hadoop namenode -format 12/05/21 06:13:51 INFO namenode.NameNode: STARTUP_MSG: /* ...

  5. PlSql加入数据库链接

    Oracle-OraDb10g_home2  ->  配置和移植工具 -> net Manager -> 本地 -> 服务命名 -> 加入(网络服务名自己命名,主机名:要 ...

  6. Live555 中的客户端openRTSP 保存H264文件

    http://amitapba.blog.163.com/blog/static/20361020720140189239762/ http://amitapba.blog.163.com/blog/ ...

  7. saltstack之定时管理

    1.设置定时任务 /srv/salt/cron/ntpdate.sls /usr/sbin/ntpdate 10.31.10.3; /sbin/hwclock -w: cron.present: - ...

  8. 五分钟上手Git

    团队开发中.遵循一个合理.清晰的Git使用流程.是非常重要的.否则,每一个人都提交一堆杂乱无章的commit,项目非常快就会变得难以协调和维护.以下是ThoughtBot 的Git使用规范流程.我从中 ...

  9. math课本复习

    第七章 微分方程 第一节 微分方程的基本概念    未知函数.未知函数的倒数与自变量之间的关系的方程,叫做微分方程. 第二节 可分离变量的微分方程 第三节 齐次方程 第四节 一阶线性微分方程 总结:任 ...

  10. iOS 运行时详解

    注:本篇文章转自:http://www.jianshu.com/p/adf0d566c887 一.运行时简介 Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行 ...