【数据结构】模拟实现LinkedList

LinkedList是双向链表结构可以适用于任意插入场景下的插入和删除,效率较高,时间复杂度为O(1)。

模拟实现

java 复制代码
public class MyLinkedList {

    static class ListNode{
        private int val;//值域
        private ListNode prev;//前驱
        private ListNode next;//后继

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//双向链表的头节点
    public ListNode last;//双向链表的尾节点
}

LinkedList常用方法

java 复制代码
//头插法
public void addFirst(int data)
    
//尾插法
public void addLast(int data)
    
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data)
    
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key)
    
//删除第一次出现关键字为key的节点
public void remove(int key)
    
//删除所有值为key的节点
public void removeAllKey(int key)
    
//得到链表的长度
public int size()
    
//清空链表
public void clear()

实现addFirst方法(头插法)

java 复制代码
public void addFirst(int data){
	ListNode node = new ListNode(data);
    //如果链表为空 插入的元素就是头节点和尾节点
	if (head==null){
		head = node;
		last = node;
	}else {
		node.next = head;//使node的后继是现在的头节点
		head.prev = node;//使现在的头节点的前驱是node
		head = node;//让node成为新的头节点
	}
}

实现addList方法(尾插法)

java 复制代码
public void addLast(int data){
	ListNode node = new ListNode(data);
    //和头插一样
	if (last==null){
		head = node;
		last = node;
	}else {
		last.next = node;//使现在尾节点的后继为node
		node.prev = last;//使node的前驱为现在的尾节点
		last = last.next;//让node成为尾节点
	}
}

实现size方法(求链表长度)

java 复制代码
public int size(){
	ListNode cur = head;
	int count = 0;
	while (cur!=null){
		count++;
		cur = cur.next;
	}
	return count;
}

实现addIndex方法(在任意位置插入元素)

java 复制代码
public void addIndex(int index,int data){
    //插入的位置如果为0 可以使用头插法
	if (index==0){
		addFirst(data);
		return;
	}
    //如果在最后一个位置插入 可以使用尾插法
	if (index==size()){
		addLast(data);
		return;
	}
    
	ListNode node = new ListNode(data);
    //判断要插入的下标是否合法
	if (index<0||index>size()){
		System.out.println("index 不合法"+index);
		return;
	}
	ListNode cur = head;
    //让cur走到要插入的位置
	while (index!=0){
		cur = cur.next;
		index--;
	}
	node.next = cur;
	cur.prev.next = node;
	node.prev = cur.prev;
	cur.prev = node;
}

实现contains方法(查找是否包含关键字key是否在单链表当中)

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

实现remove方法(删除第一次出现关键字为key的节点)

java 复制代码
public void remove(int key){
    ListNode cur = head;
    while (cur!=null){
        if (cur.val==key){
            //删除头节点
            if (cur==head){
                head = head.next;
                if (head==null){
                    //只有一个头节点
                    cur.prev=null;

                }else {
                    last=null;
                }
                }else {
                    if (cur.next!=null){
                        //删除中间节点
                        cur.prev.next=cur.next;
                        cur.next.prev=cur.prev;
                    }else {
                    //删除尾节点
                    cur.prev.next=cur.next;
                    last=last.prev;
                }
            }
            return;
        }else {
            cur=cur.next;
        }
    }
}

实现removeAllkey(删除所有值为key的节点)

java 复制代码
public void removeAllKey(int key){
    ListNode cur = head;
    while (cur!=null){
        if (cur.val==key){
            //删除头节点
            if (cur==head){
                head = head.next;
                if (head==null){
                    //只有一个头节点
                    cur.prev=null;
                }else {
                    last=null;
                }
                }else {
                    if (cur.next!=null){
                        //删除中间节点
                        cur.prev.next=cur.next;
                        cur.next.prev=cur.prev;
                	}else {
                    	//删除尾节点
                    	cur.prev.next=cur.next;
                    	last=last.prev;
                }
            }
            cur=cur.next;
        }else {
            cur=cur.next;
        }
    }
}

实现clear方法(清除链表)

java 复制代码
public void clear(){
    ListNode cur = head;
    while (cur!=null){
        ListNode curNew = cur.next;
        cur.prev=null;
        cur.next=null;
        cur = curNew;
    }
    head=null;
    last=null;
}
相关推荐
星星.7223 分钟前
2026河南萌新联赛第六场(郑州大学)补题B、D、L、J、I
数据结构·c++·算法
心仪久了会沦陷21 分钟前
数据结构入门系列——顺序表详解:概念、分类与动态实现
c语言·数据结构·经验分享·笔记·开源
smj2302_7968265224 分钟前
解决leetcode第4033题有效K个不同元素子数组I
数据结构·python·算法·leetcode
旖旎夜光1 小时前
LCR 173:在点名(二分查找) —— 题解
数据结构·c++·算法·leetcode·二分查找
重生之后端学习1 小时前
438. 找到字符串中所有字母异位词[中等]✅
开发语言·数据结构·算法·leetcode·职场和发展
LuminousCPP14 小时前
数据结构‑二叉树(二):二叉堆从零实现|Heap结构设计 + 建堆优化 + 堆排序深度解析
c语言·数据结构·笔记·二叉树
15 小时前
数据结构第一课:复杂度解析
数据结构
欧叶冲冲冲17 小时前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
_Narcissus_18 小时前
链表算法题和静态链表
数据结构·c++·笔记·算法·链表·ai·力扣
Lyyaoo.18 小时前
【普通数组】【中等】除了自身以外数组的乘积
数据结构·算法·leetcode