# C++: Combining two circular, doubly linked lists - Answer

See Problem

Given two circular, doubly linked lists, write a function that combines them.

    //--- Node for doubly linked list of floats.
    struct Node {
        Node* next;  // Pointer to next element
        Node* prev;  // Pointer to previous element
        float data;
    };

    //--- Function
    Node* splice(Node* p, Node* q) {
        //... If either is NULL, return other.
        if (p == NULL) return q;
        if (q == NULL) return p;

        //... Link q's Nodes after p. Same order is important.
        p->next->prev = q->prev;
        q->prev->next = p->next;
        p->next = q;
        q->prev = p;
    }