题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
http://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = Integer.parseInt(sc.next());
LinkedList<Integer> list = new LinkedList<>();
int head = Integer.parseInt(sc.next());
list.add(head);
for (int i = 1; i < n; i++) {
int pre = Integer.parseInt(sc.next());
int cur = Integer.parseInt(sc.next());
int indexInsert = list.indexOf(cur) + 1;
list.add(indexInsert, pre);
}
int delete = Integer.parseInt(sc.next());
list.removeIf(integer -> integer.equals(delete));
if (list.size() == 0) {
System.out.println("null");
} else {
for (Integer integer : list) {
System.out.print(integer + " ");
}
}
}
}
}