链表(单向不带头非循环)

声明

链表题考的都是单向不带头非循环,所以在本专栏中只介绍这一种结构,实际中链表的结构非常多样,组合起来就有8种链表结构。

链表的实现

创建一个链表

注意:此处简单粗暴创建的链表只是为了初学者好上手。


public class MySingleLinkedList {static class ListNode {public int val;public ListNode next;public ListNode(int val) {this.val = val;}}public ListNode head;public void createList() {ListNode node1 = new ListNode(12);ListNode node2 = new ListNode(23);ListNode node3 = new ListNode(34);ListNode node4 = new ListNode(45);ListNode node5 = new ListNode(56);node1.next = node2;node2.next = node3;node3.next = node4;node4.next = node5;this.head = node1;}
}

核心代码

1、前一个节点与后一个节点如何关联起来

node1.next=node2;

2、什么时候遍历完所有的节点

head==null

3、怎么从前一个节点走到后一个节点

head=head.next;

打印一个链表

public void display() {ListNode cur = head;while (cur != null) {System.out.print(cur.val + " ");cur = cur.next;}System.out.println();
}

获取链表的长度

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

头插

public void addFirst(int data) {ListNode node = new ListNode(data);node.next = head;head = node;
}

尾插

1、先要判断是否为空链表,不然会有空指针异常。

2、如果是空链表则将节点赋给头节点,还应当用return语句结束。

3、再找到链表的尾巴节点cur.next==null

public void addLast(int data) {ListNode node = new ListNode(data);if (head == null) {head = node;return;}ListNode cur = head;while (cur.next != null) {cur = cur.next;}cur.next = node;
}

在任意位置插入

1、先要判断插入位置是否合法

2、在头节点插入-头插(index==0),在尾节点插入-尾插(index==size)

3、找到要插入位置的前一个结点,只需要cur走index-1

4、连接的时候注意先绑后头

public class IndexNotLegalException extends RuntimeException {public IndexNotLegalException() {}public IndexNotLegalException(String msg) {super(msg);}
}
//第一个数据节点为0号下标public void addIndex(int index, int data) {try {checkIndex(index);} catch (IndexNotLegalException e) {e.printStackTrace();}if (index == 0) {addFirst(data);return;}if (index == size()) {addLast(data);return;}ListNode cur = findIndexSubOne(index);ListNode node = new ListNode(data);node.next = cur.next;cur.next = node;
}private void checkIndex(int index) throws IndexNotLegalException {if (index < 0 || index > size()) {throw new IndexNotLegalException("index不合法!");}
}private ListNode findIndexSubOne(int index) {ListNode cur = head;for (int i = 0; i < index - 1; i++) {cur = cur.next;}return cur;
}

查找是否包含某个值

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

删除第一次为某值的节点

1、如果是空链表则不可删,直接return

2、如果删掉的是头节点,head=head.next;

3、找到所要删除节点的前一个cur.next.val==key

public void remove(int key) {if (head == null)return;if (head.val == key) {head = head.next;return;}ListNode cur = head;while (cur.next != null) {if (cur.next.val == key) {cur.next = cur.next.next;return;}cur=cur.next;}
}

删除所有为某值的节点

1、同样,头节点不可删

2、cur代表当前需要删除的节点,prev代表当前需要删除节点cur的前驱节点

3、最后还要处理头节点

public void removeAllKey(int key) {if (head == null) return;ListNode prev = head;ListNode cur = head.next;while (cur != null) {if (cur.val == key) {prev.next = cur.next;} else {prev = cur;}cur = cur.next;}if (head.val == key)head = head.next;
}

清空链表

public void clear() {ListNode cur = head;while (cur != null) {ListNode curN = cur.next;cur.next = null;cur = curN;}head = null;
}

链表的使用

1. LinkedList实现了List接口

2. LinkedList的底层使用了双向链表

3. LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问

4. LinkedList的任意位置插入和删除元素时效率比较高

5. LinkedList比较适合任意位置插入的场景

LinkedList的构造

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;public class Test {public static void main(String[] args) {LinkedList<Integer> list1 = new LinkedList<>();List<Integer> list2 = new LinkedList<>();list2.add(1);list2.add(2);List<Integer> ret1 = new LinkedList<>(list2);ArrayList<Integer> arrayList=new ArrayList<>();List<Integer> ret2=new LinkedList<>(arrayList);}
}

LinkedList的其他常用方法介绍

LinkedList的遍历
 

import java.util.LinkedList;
import java.util.ListIterator;public class Test {public static void main(String[] args) {LinkedList<Integer> list = new LinkedList<>();list.add(1); // add(elem): 表示尾插list.add(2);list.add(3);list.add(4);list.add(5);list.add(6);list.add(7);System.out.println(list.size());// foreach遍历for (int e : list) {System.out.print(e + " ");}System.out.println();// 使用迭代器遍历---正向遍历ListIterator<Integer> it = list.listIterator();while (it.hasNext()) {System.out.print(it.next() + " ");}System.out.println();// 使用反向迭代器---反向遍历ListIterator<Integer> rit = list.listIterator(list.size());while (rit.hasPrevious()) {System.out.print(rit.previous() + " ");}System.out.println();}
}

ArrayList和LinkedList的区别

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1541600.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Spring(三)Spring事件+计划任务+条件注解+SpringAware

Application Event 事件 当一个Bean处理完一个任务之后&#xff0c;希望另一个Bean知道并做出相应的处理&#xff0c;这时需要让另外一个Bean监听当前Bean所发送的事件 自定义事件&#xff0c;集成ApplicationEvent自定义事件监听器&#xff0c;实现ApplicationListener使用容…

S-Clustr-Simple 飞机大战:骇入现实的建筑灯光游戏

项目地址:https://github.com/MartinxMax/S-Clustr/releases Video https://www.youtube.com/watch?vr3JIZY1olro 飞机大战 这是一个影子集群的游戏插件&#xff0c;可以将游戏画面映射到现实的设备&#xff0c;允许恶意控制来完成游戏。亦或者设备部署在某建筑物中,来控制…

电脑硬件-机械硬盘

简介 机械硬盘是电脑的主要存储媒介之一&#xff0c;通常用于存储一些文件资料或者学习视频笔记等比较大的内容。 结构 采用磁盘存储数据&#xff0c;使用温彻斯特的结构&#xff0c;特有四个特点&#xff1a; 1.磁头、盘片和运动机构安装在一个密封的腔体内。 2.盘片告诉旋…

AI大模型算法工程师经典面试题————为什么 Bert 的三个 Embedding 可以进行相加?

大模型算法工程师经典面试题————为什么 Bert 的三个 Embedding 可以进行相加&#xff1f; 为什么 Bert 的三个 Embedding 可以进行相加&#xff1f; Token Embedding、Segment Embedding、Position Embedding的意义我已经清楚了&#xff0c;但是这三个向量为什么可以相加…

数据中台系统产品原型RP原型Axure高保真交互原型 源文件分享

在数字化时代&#xff0c;数据已经成为企业最宝贵的资产之一。为了更好地管理和利用这些数据&#xff0c;这边为大家整理了一套数据中台Axure高保真原型。这套原型致力于为企业提供全方位的数据服务&#xff0c;助力企业实现数据驱动的创新发展。 下载及预览地址&#xff1a;h…

MATLAB智能优化算法-学习笔记(3)——大规模邻域搜索算法求解旅行商问题【过程+代码】

一、问题描述 旅行商问题(TSP, Traveling Salesman Problem)是组合优化中的经典问题之一。给定一组城市和每对城市之间的距离,要求找到一条最短的路径,使旅行商从某个城市出发,访问每个城市一次并最终回到出发点。TSP问题广泛应用于物流配送、工厂调度、芯片制造等领域。…

1、等保测评介绍

数据来源&#xff1a;等保测评基础知识学习(1.02.0)2024最新版_哔哩哔哩_bilibili 等级保护的定义&#xff1a; 对国家秘密信息、法人或其他组织及公民专有信息以及公开信息&#xff0c;按照其重要程度对信息系统实施分等级安全保护。这包括对使用的安全产品进行等级管理&…

基于协同过滤算法的商品推荐系统

系统展示 用户前台界面 管理员后台界面 商家后台界面 系统背景 随着互联网技术的飞速发展&#xff0c;用户每天面临的信息量呈爆炸式增长&#xff0c;如何有效地筛选出用户感兴趣的内容成为一大挑战。在此背景下&#xff0c;基于协同过滤算法的商品推荐系统应运而生。该系统通过…

AI Agent,将如何打破大模型的应用边界?

大语言模型的浪潮&#xff0c;推进了AlAgent落地 上个世纪50年代&#xff0c;阿兰图灵首次将"高度智能有机体"的概念提出。经过半个多世纪的发展&#xff0c;终于在2023年进入了一个新的高潮&#xff0c;并于今年进入了爆发阶段。 自2022年11月30日chatGPT发布以来…

linux下共享内存的3种使用方式

进程是资源封装的单位&#xff0c;内存就是进程所封装的资源的一种。一般情况下&#xff0c;进程间的内存是相互隔离的&#xff0c;也就是说一个进程不能访问另一个进程的内存。如果一个进程想要访问另一个进程的内存&#xff0c;那么必须要进过内核这个桥梁&#xff0c;这就是…

工业机器视觉中的常见需求

目录 学习目的 熟系 Halcon的原因 专业性强&#xff1a; 高性能&#xff1a; 丰富的功能库 学习 OpenCV 的原因 开源与免费&#xff1a; 灵活性与可扩展性&#xff1a; 广泛的应用&#xff1a; 学习资源丰富&#xff1a; 总结 学习背景 工业视觉检测中常见分类 一、定…

【我的 PWN 学习手札】tcache stash with fastbin double free —— tcache key 绕过

参考看雪课程&#xff1a;PWN 探索篇 前言 tcache key 的引入使得 tcache dup 利用出现了困难。除了简单利用 UAF 覆写 key 或者House Of Karui 之外&#xff0c;还可以利用 ptmalloc 中的其他机制进行绕过。 一、Tcache Stash with Fastbin Double Free 之前是 double free …

实景三维+耕地保护:构建耕地资源管理的全闭环新模式

在耕地资源日益珍贵的今天&#xff0c;如何高效、精准地实施耕地保护&#xff0c;成为了我国农业可持续发展与生态文明建设的关键课题。“实景三维耕地保护”的创新模式&#xff0c;能够为这一挑战提供突破性的解决方案&#xff0c;打造一个从前端监测到后端管理的全闭环耕地保…

【Delphi】Delphi 中的 LiveBindings 使用场景与概念

LiveBindings 是 Delphi 提供的一种数据绑定机制&#xff0c;用于将 UI 控件与数据源&#xff08;如数据库字段、对象属性等&#xff09;进行动态连接。LiveBindings 允许开发人员通过可视化的方式绑定数据&#xff0c;省去了大量的手动编写代码&#xff0c;使 UI 更新和数据同…

大数据实验2.Hadoop 集群搭建(单机/伪分布式/分布式)

实验二&#xff1a; Hadoop安装和使用 一、实验目的 实现hadoop的环境搭建和安装Hadoop的简单使用&#xff1b; 二、实验平台 操作系统&#xff1a;Linux&#xff08;建议Ubuntu16.04或者18.04&#xff09;&#xff1b;Hadoop版本&#xff1a;3.1.3&#xff1b;JDK版本&…

Linux命令:用于创建新的用户组的命令行工具groupadd 详解

目录 一、概述 二、组标识符GID 1、定义 &#xff08;1&#xff09;标识符 &#xff08;2&#xff09;与UID的关系 2、GID的作用 &#xff08;1&#xff09;用户组管理 &#xff08;2&#xff09;文件权限控制 &#xff08;3&#xff09;用户权限管理 &#xff08;4&…

爱心代码(简单免费可直接运行)

代码展示&#xff08;可私信了解更多&#xff09; #include<stdio.h > #include<stdlib.h > #include<windows.h> int main(int argc, char* argv[]) {float x, y, a;for (y 1.5; y > -1.5; y - 0.1) {for (x -1.5; x < 1.5; x 0.05){a x * x y…

61. 旋转链表【 力扣(LeetCode) 】

零、原题链接 61. 旋转链表 一、题目描述 给你一个链表的头节点 head &#xff0c;旋转链表&#xff0c;将链表每个节点向右移动 k 个位置。 二、测试用例 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,4,5], k 2 输出&#xff1a;[4,5,1,2,3]示例 2&#xff1a; 输入…

ftrace - 几种tracer的打印例子

ftrace - Function Tracer — The Linux Kernel documentation【原创】Ftrace使用及实现机制 - 沐多 - 博客园 (cnblogs.com) latency format nop tracer和function tracer下&#xff0c;latency format的时间戳是相对开始trace的时间&#xff0c;non-latency format的时间戳是…

堆-使用offer创建堆和使用heapify创建堆的时间复杂度+堆排序

一、创建堆的时间复杂度比较 1、使用offer创建堆&#xff1a;时间复杂度为&#xff0c;其中n为满二叉树的结点数 核心代码&#xff1a; /*** 上浮* param childIndex*/private void floatUp(int childIndex){int parentIndexgetParentIndex(childIndex);int currIndexchildI…