基于#派仔#的修改
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { private class Node{ int val; Node next; Node(int val){ this.val=val; } } private Node head; public Main(int val){ head=new Node(val); } public void insert(int val,int node){ Node p=head; while(p.val !=node){ p=p.next; } Node newNode=new Node(val); newNode.next=p.next; p.next=newNode; } public void delete(int node){ if(node==head.val){ head=head.next; return; } Node p=head; while(node !=p.next.val){ p=p.next; } if(p.next.next==null){ p.next=null; return; } p.next=p.next.next; } public Node head(){ return this.head; } public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { // 注意 while 处理多个 case int n=in.nextInt(); int val=in.nextInt(); Main solution=new Main(val); for(int i=0;i<n-1;i++){ int value=in.nextInt(); int index=in.nextInt(); solution.insert(value,index); } val=in.nextInt(); solution.delete(val); Node p=solution.head(); while(p!=null){ System.out.print(p.val+" "); p=p.next; } System.out.println(); } } }