题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
import java.util.*; import java.lang.*; // 注意类名必须为 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 int num = in.nextInt(); int first = in.nextInt(); LinkedList<Integer> ll = new LinkedList<>(); int m0 = in.nextInt(); int m1 = in.nextInt(); ll.add(m1); ll.add(m0); for (int i = 2; i < num; i++) { int n0 = in.nextInt(); int n1 = in.nextInt(); int ind = ll.indexOf(n1); if (ind != -1) { ListIterator<Integer> li = ll.listIterator(ind); li.next(); li.add(n0); } else if (ind == -1) { ll.add(n1); ll.add(n0); } } int del = in.nextInt(); ll.remove(ll.indexOf(del)); ll.stream().forEach(k->System.out.printf("%d ", k)); } } }