import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @描述: 反转单向链表
* @思路: 涉及三个节点, preNode、currode、nextNode
* 1.声明preNode nextNode;
* 2.反转preNode、currNode顺序
* 3.移动指针,更新preNode、currNode
* @复杂度:时间O(N)
* @链接:https://www.nowcoder.com/practice/b66a251dec8847f386bbe6cd96b7e9c8?tpId=101&tqId=33175&tPage=1&rp=1&ru=/ta/programmer-code-interview-guide&qru=/ta/programmer-code-interview-guide/question-ranking
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(input.readLine());
String[] strings1 = input.readLine().split(" ");
Node head1 = Node.createNodeList(strings1);
head1 = ReverseList.reverseList(head1);
int m = Integer.parseInt(input.readLine());
String[] strings2 = input.readLine().split(" ");
DoubleNode head2 = DoubleNode.createNodeList(strings2);
head2 = ReverseList.reverseDoubleList(head2);
Node.printNodeList(head1);
DoubleNode.printNodeList1(head2);
}
}
class ReverseList {
public static Node reverseList(Node head) {
Node node = head;
Node pre = null; //初始化preNode
Node next = null;
while (node != null) {
next = node.getNext(); //初始化nextNode
//反转顺序
node.setNext(pre);
//移动
pre = node; //pre 当前节点
node = next;
}
return pre;
}
public static DoubleNode reverseDoubleList(DoubleNode head) {
DoubleNode node = head;
DoubleNode pre = null; //初始化preDoubleNode
DoubleNode next = null;
while (node != null) {
next = node.getNext(); //初始化nextDoubleNode
//反转顺序
node.setNext(pre);
node.setLast(next);
//移动
pre = node; //pre 当前节点
node = next;
}
return pre;
}
}
class Node {
private Node next;
private int value;
public Node(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public static Node createNodeList(Integer[] values) {
Node head = new Node((values[0]));
Node node = head;
for (int i = 1; i < values.length; i++) {
Node newNode = new Node(values[i]);
node.next = newNode;
node = newNode;
}
return head;
}
public static Node createNodeList(String[] values) {
Node head = new Node(Integer.parseInt(values[0]));
Node node = head;
for (int i = 1; i < values.length; i++) {
Node newNode = new Node(Integer.parseInt(values[i]));
node.next = newNode;
node = newNode;
}
return head;
}
public static void printNodeList(Node head) {
StringBuilder sb = new StringBuilder();
while (head != null) {
sb.append(head.getValue()).append(" ");
head = head.getNext();
}
System.out.println(sb.toString());
}
}
class DoubleNode {
private DoubleNode last;
private DoubleNode next;
private int value;
public DoubleNode(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public DoubleNode getNext() {
return next;
}
public void setNext(DoubleNode next) {
this.next = next;
}
public DoubleNode getLast() {
return last;
}
public void setLast(DoubleNode last) {
this.last = last;
}
public static DoubleNode createNodeList(String[] values) {
DoubleNode head = new DoubleNode(Integer.parseInt(values[0]));
DoubleNode node = head;
DoubleNode pre = null;
for (int i = 1; i < values.length; i++) {
DoubleNode newNode = new DoubleNode(Integer.parseInt(values[i]));
node.next = newNode;
node.last = pre;
pre = node;
node = newNode;
}
return head;
}
public static DoubleNode createNodeList(Integer[] values) {
DoubleNode head = new DoubleNode((values[0]));
DoubleNode node = head;
DoubleNode pre = null;
for (int i = 1; i < values.length; i++) {
DoubleNode newNode = new DoubleNode(values[i]);
node.next = newNode;
node.last = pre;
pre = node;
node = newNode;
}
return head;
}
public static void printNodeList1(DoubleNode head) {
StringBuilder sb = new StringBuilder();
while (head != null) {
sb.append(head.getValue()).append(" ");
head = head.getNext();
}
System.out.println(sb.toString());
}
public static void printNodeList2(DoubleNode last) {
StringBuilder sb = new StringBuilder();
while (last != null) {
sb.append(last.getValue()).append(" ");
last = last.getLast();
}
System.out.println(sb.toString());
}
}