(有头)链表的实现(Java)

一、初始化

java 复制代码
public class LinkedList {

    public class ListNode{
        int val = 0;
        ListNode next = null;
        public ListNode(int val){
            this.val = val;
            this.next = null;
        }
    }
    private ListNode head;
    private int size = 0;
    public LinkedList(){
        head = new ListNode(0);
        this.size = 0;
    }
}

二、头插

java 复制代码
    public void addFirst(int val){
        ListNode newNode = new ListNode(val);
        newNode.next = head.next;
        head.next = newNode;
        size++;
    }

三、尾插

java 复制代码
    public void addLast(int val){
        ListNode newNode = new ListNode(val);
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = newNode;
        size++;
    }

四、指定位置插入(下标)

java 复制代码
    public void addIndex(int index, int val){
        if(index < 0 || index > size){
            return;
        }
        ListNode newNode = new ListNode(val);
        ListNode cur = head;
        while(index-- > 0){
            cur = cur.next;
        }
        newNode.next = cur.next;
        cur.next = newNode;
        size++;
    }

五、是否包含某个key

java 复制代码
    public boolean contains(int val){
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

六、删除某个节点

java 复制代码
    public void remove(int val){
        ListNode cur = head.next;
        ListNode prev = head;
        while(cur != null){
            if(cur.val == val){
                prev.next = cur.next;
                size--;
                break;
            }
            prev = cur;
            cur = cur.next;
        }
    }

七、删除所有的key

java 复制代码
    public void removeAllKeys(int val){
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == val){
                cur.next = cur.next.next;
            }else{
                cur = cur.next;
            }
        }
    }

八、size、打印、清理

java 复制代码
    public int size(){
        return size;
    }
    public void display(){
        ListNode cur = head.next;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    public void clear(){
        head.next = null;
        size = 0;
    }
相关推荐
xieliyu.25 分钟前
Java算法精讲:双指针(三)
java·开发语言·算法
明夜之约40 分钟前
Spring Boot 自动装配源码
java·spring boot·后端
Leaton Lee41 分钟前
Spring Boot分层架构详解:从Controller到Service再到Mapper的完整流程
java·spring boot·后端·架构
Jinkxs43 分钟前
Resilience4j- 与 Spring Boot 快速集成:自动配置与基础注解使用
java·spring boot·后端
辣机小司44 分钟前
【踩坑记录:Spring Boot 配置文件读取值不一致?警惕 YAML 的“八进制陷阱”与 SnakeYAML 版本之谜】
java·spring boot·后端·yaml·踩坑记录
fangdengfu1232 小时前
ES分析系统各个服务日志占用量
java·前端·elasticsearch
云烟成雨TD2 小时前
Spring AI 1.x 系列【51】可观测性技术选型
java·人工智能·spring
星越华夏2 小时前
ESP32-CAM图像传输项目说明文档
java·后端·struts·esp32
cfm_29142 小时前
Redis五大基本数据结构底层了解
数据结构·数据库·redis
如竟没有火炬2 小时前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵