#include<stdio.h>
#include<stdlib.h>
typedef struct node{
    int data;
    struct node*prev;
    struct node*next;
} Node;
Node*head=NULL;
Node*tail=NULL;
void append(int value){
    node*newnode=(node*)malloc(sizeof(node));
    if(newnode==NULL){
        exit(EXIT_FAILURE);
    }
    newnode->data=value;
    newnode->next=NULL;
    newnode->prev=tail;
    if(head==NULL){
        head=newnode;
        tail=newnode;
    }else{
        tail->next=newnode;
    }
}
void deletelastnode(){
    return;
}
if(head==tail){
    free(head);
    head=NULL;
    tail=NULL;
    return;
}
node*temp=tail;
tail=tail->prev;
free(temp);
}
void printlist(){
    node*current=head;
    while(current !=NULL){
        printf("%d",current->data);
        if(current->next!=NULL){
            printf(" ");
        }
        current=current->next;
    }
    printf("\n");
}
void freelist(){
    node*current=head;
    node*next;
    while(current!=NULL){
        next=current->next;
        free(current);
        current=next;
    }
    head=NULL;
    tail=NULL;
}
int main(){
    int n;
    int value;
    if(scanf("%d",&n)!=1){
        return 1;
    }
    if(n<=0){
        printf("Invalid input\n");
        return 0;
    }
    for(int i=0;i<n;i++){
        if(scanf("%d",&value)!=1){
            freelist();
            return 1;
        }
        append(value);
    }
    printlist();
    deletelastnode();
    printlist();
    freelist();
    return 0;
}