zoukankan      html  css  js  c++  java
  • 430. Flatten a Multilevel Doubly Linked List

    /*
    // Definition for a Node.
    class Node {
    public:
        int val = NULL;
        Node* prev = NULL;
        Node* next = NULL;
        Node* child = NULL;
    
        Node() {}
    
        Node(int _val, Node* _prev, Node* _next, Node* _child) {
            val = _val;
            prev = _prev;
            next = _next;
            child = _child;
        }
    };
    */
    class Solution {
    public:
        Node* flatten(Node* head) {
            if (head == NULL)   return head;
            flatten2(head);
            return head;
        }
        Node* flatten2(Node* head) {    // flatten and return tail
            Node* ret = head;
            while (head) {
                ret = head;
                if (head->child) {
                    Node* tail = flatten2(head->child);
                    tail->next = head->next;
                    if (tail->next)
                        tail->next->prev = tail;
                    head->next = head->child;
                    head->next->prev = head;
                    head->child = NULL;
                    head = tail->next;
                    ret = tail;
                }
                else {
                    head = head->next;
                }
            }
            return ret;
        }
    };
  • 相关阅读:
    bzoj1914
    bzoj3144
    bzoj2756
    poj3177
    一些比较水的题目
    bzoj2282
    屯题50AC纪念
    Base64解码中文部分中文乱码的原因
    随机生成36位字符串
    jQuery判断某个元素是否存在某个样式
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/9988957.html
Copyright © 2011-2022 走看看