题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
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().split(" ");
int N = Integer.parseInt(str[0]);
List<Integer> list = new ArrayList<>();
list.add(Integer.parseInt(str[1]));
for(int i = 2;i<str.length-1;i=i+2){
List<Integer> list2 = new ArrayList<>();
for(int b:list){
list2.add(b);
if(Integer.parseInt(str[i+1])==b) list2.add(Integer.parseInt(str[i]));
}
list = list2;
}
List<Integer> list2 = new ArrayList<>();
for(int b:list){
if(b!=Integer.parseInt(str[str.length-1])) list2.add(b);
}
for(int b:list2){
System.out.print(b+" ");
}
}
}
}
查看6道真题和解析