import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); short totalNode = Short.parseShort(scanner.next()); CustomNode firstNode = new CustomNode(); firstNode.value = Short.parseShort(scanner.next()); // 构造链表数据 for (int i = 0; i < totalNode - 1; i++) { short num = Short.parseShort(scanner.next()); short behindNodeValue = Short.parseShort(scanner.next()); CustomNode newNode = new CustomNode(); newNode.value = num; CustomNode searchNode = firstNode; if (searchNode.value == behindNodeValue) { newNode.next = searchNode.next; searchNode.next = newNode; } else { while (searchNode != null) { if (searchNode.value == behindNodeValue) { newNode.next = searchNode.next; searchNode.next = newNode; break; } searchNode = searchNode.next; } } } short deleteNum = Short.parseShort(scanner.next()); // 删除节点 CustomNode searchNode = firstNode; CustomNode lastSearchNode = firstNode; while (searchNode != null) { if (searchNode.value == deleteNum) { // 删除该节点 if (searchNode == lastSearchNode) { // 如果删除的是头节点,直接把头节点删了就好 firstNode = firstNode.next; } else { lastSearchNode.next = searchNode.next; searchNode.next = null; } } lastSearchNode = searchNode; searchNode = searchNode.next; } // 输出 searchNode = firstNode; while (searchNode != null) { System.out.print(searchNode.value + " "); searchNode = searchNode.next; } } } class CustomNode { public short value; public CustomNode next; }