(有头)链表的实现(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;
    }
相关推荐
进击的荆棘25 分钟前
C++起始之路——哈希表的实现
数据结构·c++·散列表·哈希
Rsun0455125 分钟前
为什么要配置maven
java·maven
人道领域30 分钟前
【Redis实战篇】初步基于Redis实现的分布式锁---基于黑马点评
java·数据库·redis·分布式·缓存
呱牛do it5 小时前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 3)
java·vue
神の愛6 小时前
左连接查询数据 left join
java·服务器·前端
南境十里·墨染春水7 小时前
linux学习进展 线程同步——互斥锁
java·linux·学习
雨奔7 小时前
Kubernetes 联邦 Deployment 指南:跨集群统一管理 Pod
java·容器·kubernetes
杨凯凡7 小时前
【021】反射与注解:Spring 里背后的影子
java·后端·spring
lulu12165440787 小时前
Claude Code项目大了响应慢怎么办?Subagents、Agent Teams、Git Worktree、工作流编排四种方案深度解析
java·人工智能·python·ai编程
riNt PTIP7 小时前
SpringBoot创建动态定时任务的几种方式
java·spring boot·spring