#include<iostream>usingnamespace std;// define the ListNode classclassListNode{public:int value;// the value of the node
ListNode* next;// the pointer to the next node// constructor to initialize the nodeListNode(int val){
value = val;
next =nullptr;}};intmain(){int length;
cin >> length;int headValue;
cin >> headValue;// create the head node using the class constructor
ListNode *head =newListNode(headValue);// create a dummy head node that points to the head node
ListNode *dummy =newListNode(0);
dummy->next = head;
length--;while(length--){int curV;int nextV;
cin >> nextV >> curV;// find the node with curV using the dummy head
ListNode *cur = dummy;while(cur->next !=nullptr&& cur->next->value != curV){
cur = cur->next;}// insert a new node with nextV after cur
ListNode *temp =newListNode(nextV);
temp->next = cur->next;
cur->next = temp;}// delete the node with toDel using the dummy headint toDel;
cin >> toDel;
ListNode *cur = dummy;while(cur->next !=nullptr){if(cur->next->value == toDel){// delete the node by skipping it
cur->next = cur->next->next;}else{// move to the next node
cur = cur->next;}}// print the list values using the head node
head = dummy->next;// skip the dummy headwhile(head !=nullptr){
cout << head->value <<" ";
head = head->next;}}