蛋疼的递归删除无头单链表元素

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by yueli on 2018/9/11.
 5  */
 6 public class Exmple {
 7     class node{
 8         public int n;
 9         public node next;
10     }
11     public void remove(int x,node now,node prev){
12         if(now!=null){
13             remove(x,now.next,now);
14             if(now.n==x) {
15                 prev.next = now.next;
16                 if (now==list){
17                     list=list.next;
18                 }
19             }
20         }
21     }
22     public node list;
23     Scanner scanner;
24     Exmple(){
25        // scanner=new Scanner(System.in);
26         int a[]={1,2,8,7,6};
27         list=new node();
28         list.n=8;
29         node p=list;
30         for(int i=0;i<5;i++){
31             node temp=new node();
32             temp.n=a[i];
33             p.next=temp;
34             p=p.next;
35         }
36     }
37     public void show(){
38         node p=list;
39         while (p!=null){
40             System.out.print(p.n);
41             p=p.next;
42         }
43     }
44     public void showRemove(int x){
45         remove(x,list,list);
46         System.out.println();
47         show();
48     }
49     public static void main(String[] args) {
50          Exmple e=new Exmple();
51          e.show();
52          e.showRemove(8);
53     }
54 }
原文地址:https://www.cnblogs.com/yuelien/p/9631595.html