题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
http://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
import java.util.*;
public class Main {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int total = sc.nextInt();
int head = sc.nextInt();
List<Integer>link = new ArrayList<>();
link.add(head);
for(int i = 0; i < total-1;i++){
int value = sc.nextInt();
int target = sc.nextInt();
link.add(link.indexOf(target)+1,value);
}
int delete = sc.nextInt();
link.remove(link.indexOf(delete));
for(int i : link){
System.out.print(i+" ");
}
}
}
}
public class Main {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int total = sc.nextInt();
int head = sc.nextInt();
List<Integer>link = new ArrayList<>();
link.add(head);
for(int i = 0; i < total-1;i++){
int value = sc.nextInt();
int target = sc.nextInt();
link.add(link.indexOf(target)+1,value);
}
int delete = sc.nextInt();
link.remove(link.indexOf(delete));
for(int i : link){
System.out.print(i+" ");
}
}
}
}