#include<iostream>
using namespace std;
struct Node
{
	int data;
	Node* next;
	Node* prev;
};/*
   Reverse a doubly linked list, input list may also be empty
   Node is defined as
   struct Node
   {
     int data;
     Node *next;
     Node *prev
   }
*/
Node* Reverse(Node *head)
{
    if (head == NULL || head->next == NULL) {
        return head;
    }
    Node *prev = NULL;
    Node *cur = head;
    while (cur->next != NULL) {
        Node *next = cur->next;
        cur->next = prev;
        if (prev != NULL) {
            prev->prev = cur;
        }
        prev = cur;
        cur = next;
    }
    cur->prev = NULL;
    cur->next = prev;
    prev->prev = cur;
    return cur;
}
Node* Insert(Node *head, int data)
{
	Node *temp = new Node();
	temp->data = data; temp->prev = NULL; temp->next = NULL;
	if(head == NULL) return temp;
	head->prev = temp;
	temp->next = head;
	return temp;
}
void Print(Node *head) {
	if(head == NULL) return;
	while(head->next != NULL){ cout<<head->data<<" "; head = head->next;}
	cout<<head->data<<" ";
	while(head->prev != NULL) { cout<<head->data<<" "; head = head->prev; }
	cout<<head->data<<"\n";
}
int main()
{
	int t; cin>>t;
	Node *head = NULL;
	while(t--) {
	   int n; cin>>n;
           head = NULL;
	   for(int i = 0;i<n;i++) {
		   int x; cin>>x;
		   head = Insert(head,x);
	   }
	   head = Reverse(head);
	   Print(head);
	}
}
Add the solution to "Reverse a doubly linked list".
Add the solution to "Reverse a doubly linked list".
