涉及链表的操作可以用LinkedList,其底层就是用链表实现的。

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int head = in.nextInt();
            List<Integer> list = new LinkedList<>();
            list.add(head);
            for(int i =0;i<n-1;++i){
                int insertnum = in.nextInt();
                int pre = in.nextInt();
                for(int j = 0;j<list.size();++j){
                    if(list.get(j)==pre){
                        list.add(j+1,insertnum);
                        break;
                    }
                    
                }
            }
            int deletenum = in.nextInt();
            for(Integer i:list){
                if(i!=deletenum)
                    System.out.print(i+" ");
            }
        }
    }
**}**