Skip to content

Latest commit

 

History

History
49 lines (39 loc) · 1.03 KB

File metadata and controls

49 lines (39 loc) · 1.03 KB
    void construct(int t, Node* &head, Node* &tail) {
        Node* node = newNode(t);
        if (head == NULL) {
            head = node;
            tail = node;
        } else {
            tail->next = node;
            tail = node;
        }
    }

    static bool comp(int a, int b) {
        return a > b;
    }
    
    void store(Node* root,vector<int>&v)
    {
        Node *node=root;
        while(node)
        {
            v.push_back(node->data);
            node=node->next;
        }
    }
    
    struct Node * mergeResult(Node *node1,Node *node2)
    {
        vector<int> v;
        
        store(node1,v);
        store(node2,v);
        
        sort(v.begin(),v.end(),comp);
        
        Node* head=NULL;
        Node* tail=NULL;
        
        
        for(auto it:v)
        {
            construct(it,head,tail);
        }
        return head;
    }