【数据结构】模拟实现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;
}
相关推荐
Ada's5 小时前
【计算机基础系列】003:Python数据结构
开发语言·数据结构·python
wanderist.8 小时前
线性筛法详解:从筛质数到欧拉函数、Möbius 函数与约数函数
java·数据结构·算法
All for pursuit.9 小时前
【设计-1】208.实现Trie (前缀树)
数据结构·c++·算法·leetcode
程序员清风10 小时前
哈希表:从键值映射到高效查找
数据结构·哈希算法·散列表
Logic10113 小时前
C语言/数据结构字符串题解:最短循环节——找出能重复构成原串的最短基础子串
c语言·数据结构·字符串·枚举·时间复杂度·算法题·kmp优化
垆边人似月.14 小时前
城市连通性(200分 / 并查集)
数据结构·算法·图论
Gust of wind14 小时前
串与KMP模式匹配:存储结构、基本操作、next数组手算
c语言·数据结构·后端·算法
by2099915 小时前
list:迭代器与资源管理实现的综合叙述(下)
数据结构·c++·笔记·list
我不会起名字32215 小时前
二叉树的遍历与二叉搜索树:把递归、迭代、还原树一次讲透
数据结构·leetcode·二叉树·二叉搜索树
yi01116 小时前
DAY22: LeetCode 733:图像渲染——从二维网格开始理解 DFS 和 BFS
数据结构·笔记·python·算法·leetcode·深度优先·宽度优先