题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String str = in.nextLine(); String[] strs = str.split(" "); int n = Integer.parseInt(strs[0]); int firstValue = Integer.parseInt(strs[1]); int aimValue = Integer.parseInt(strs[strs.length - 1]); LinkedList<Integer> list = new LinkedList<Integer>(); list.add(firstValue); for (int i = 2; i < strs.length - 1; i += 2) { int position = list.indexOf(Integer.parseInt(strs[i + 1])); list.add(position, Integer.parseInt(strs[i])); } list.remove(list.indexOf(aimValue)); Collections.reverse(list); for(Integer i : list) System.out.print(i+" "); } } }