一、题目
二、代码
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int num = in.nextInt(); // 链表节点个数
int firstNode = in.nextInt(); // 头节点的值
List<Integer> list = new ArrayList<>();
list.add(firstNode);
for (int i = 0; i < num-1; i++) {
int newNode = in.nextInt(); // 要插入的新节点
int oldNode = in.nextInt(); // 被插入的节点
int index = list.indexOf(oldNode); // 旧节点在链表中的下标位置
list.add(index+1, newNode);
}
Object deleteNode = in.nextInt();
list.remove(deleteNode);
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+ " ");
}
}
}
}