数据结构 -- 链表

1. 链表 -- 增

2. 链表 -- 删

3. 链表 -- 遍历

4. 链表 -- 反向遍历

5. 链表 -- 反转链表

DEMO : 

复制代码
package lime.xiaoniu;

/**
 * Created by LimeOracle on 2017/11/17.
 */
public class DevilList {
    public static void main(String[] args){
        DevilList list = null;
        for(int i = 1;i < 11;i++){
            list = DevilList.addList(list, i);
        }
        for(int i = 10;i > 0;i--) {
            list = deleteList(list, i);
            DevilList.inOrder(list);
            System.out.println();
        }
    }
    private Integer data;
    private DevilList next;

    public DevilList(Integer data) {
        this.data = data;
    }
    //添加
    public static DevilList addList(DevilList head , Integer data){
        if(null == head){
            return new DevilList(data);
        }
        head.next = addList(head.next,data);
        return head;
    }
    //遍历
    public static void inOrder(DevilList head){
        if(null == head){
            return;
        }
        System.out.print(head.data + "  ");
        inOrder(head.next);
    }
    //反向遍历
    public static void reversalData(DevilList head){
        if(null == head){
            return;
        }
        reversalData(head.next);
        System.out.print(head.data + "  ");
    }
    //反转
    public static DevilList reversalList(DevilList head){
        if(null == head || null == head.next){
            return head;
        }
        DevilList reversalHead = reversalList(head.next);
        head.next.next = head;
        head.next = null;
        return reversalHead;
    }
    //删除
    public static DevilList deleteList(DevilList head,int data){
        if(null == head){
            return head;
        }
        if(data == head.data){
            head = head.next;
        }else{
            head.next = deleteList(head.next,data);
        }
        return head;
    }



    @Override
    public String toString() {
        return "ReversalList{" +
                "data=" + data +
                ", next=" + next +
                '}';
    }
}
复制代码

啦啦啦

posted @   limeOracle  阅读(443)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示