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


     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 }
  • 相关阅读:
    python操作Redis详解
    python操作SQLAlchemy
    设计模式详解及Python实现
    计算机基础
    DRF内置过滤组件与排序组件结合使用
    LTE
    LTE
    LTE
    LTE
    LTE
  • 原文地址:https://www.cnblogs.com/yuelien/p/9631595.html
Copyright © 2020-2023  润新知