题解 | #链表#
从单向链表中删除指定值的节点
http://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//System.out.println(scanner.next());
int nodes=scanner.nextInt();
LinkedList<String> list=new LinkedList<String>(); list.add(String.valueOf(scanner.next())); //list.add("(String)scanner.next()"); int start,end; int i=0; for(i=1;i<nodes;i=i+1){ start=scanner.nextInt(); end=scanner.nextInt(); //if(list.contains(String.valueOf(end))) int index=list.indexOf(String.valueOf(end)); //System.out.println(String.valueOf(index)); list.add(index+1,String.valueOf(start)); // System.out.println(end+"->"+start); //} } String del=String.valueOf(scanner.nextInt()); i=0; for(i=0;i<nodes;++i){ if(list.get(i).equals(del)){ // i=i+1; continue; }else {
// if(i==nodes-1){
// System.out.print(i);
// break;
// }
// i=i+1
System.out.print(list.get(i)+" ");
}
}
}
}