(有头)链表的实现(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;
    }
相关推荐
yuzhiboyouye2 小时前
《阿里巴巴 Java 开发手册》的 MySQL 核心规范
java·开发语言·mysql
秋饼3 小时前
Spring AI 2.0 渐进式工具披露实战:ToolSearchToolCallingAdvisor 源码机制、四种 ToolIndex 选型与多租户踩坑
java·ai·技术分享·后端开发
让我们一起加油好吗5 小时前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
路灯中转站5 小时前
数据结构C语言版(什么是数据类型)
数据结构
凌冰_5 小时前
数据结构 - 队列(五)
数据结构·算法·软件工程
让我们一起加油好吗6 小时前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
头发还在的女程序员6 小时前
家政小程序系统源码IM已读记录:多端阅读状态怎样保持单调推进
java·spring boot·源码·家政·消息同步
Rain的Java大神之路7 小时前
别再背参数了:Kafka、RocketMQ、RabbitMQ 的设计取舍与生产级避坑指南
java·后端·rocketmq
让我们一起加油好吗7 小时前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
Rain的Java大神之路7 小时前
LeetCode 42「接雨水」:面试官百出不厌的题,一文讲透从暴力到双指针 O(1)
java·后端·leetcode