当前位置:网站首页>手写链表~内含单向链表和双向链表,请大家放心食用
手写链表~内含单向链表和双向链表,请大家放心食用
2022-04-22 20:55:00 【AI很不错呦】
力扣707题
单向链表
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class MyLinkedList {
int size;
ListNode head;
public MyLinkedList() {
size = 0;
head = new ListNode(0);
}
public int get(int index) {
//无效索引,返回-1
if(index < 0 || index >= size) {
return -1;
}
//寻找节点值
ListNode cur = head;
for(int i = 0; i < index + 1; i++) {
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
//相当于在索引为0的链表处添加了一个节点
addAtIndex(0, val);
}
public void addAtTail(int val) {
//相当于在索引为size的链表处添加了一个节点
addAtIndex(size, val);
}
public void addAtIndex(int index, int val) {
//添加节点位置 大于 链表索引
if(index > size) {
return;
}
//添加索引 小于 0, 那就给它算作是0
if(index < 0) {
index = 0;
}
size++;
//寻找要添加索引的前一个 节点
ListNode pre = head;
for(int i = 0; i < index; i++) {
pre = pre.next;
}
//添加节点
ListNode toAdd = new ListNode(val);
toAdd.next = pre.next;
pre.next = toAdd;
}
public void deleteAtIndex(int index) {
//无效索引
if(index < 0 || index >= size) {
return;
}
size--;
//寻找要删除节点的前一个结点
ListNode pre = head;
for(int i = 0; i < index; i++) {
pre = pre.next;
}
//删除操作
pre.next = pre.next.next;
}
}
双向链表
public class ListNode {
int val;
ListNode next;
ListNode pre;
ListNode(int x) {
val = x;
}
}
class MyLinkedList {
int size;
ListNode head;
ListNode tail;
public MyLinkedList() {
size = 0;
head = new ListNode(0);
tail = new ListNode(0);
head.next = tail;
tail.pre = head;
}
public int get(int index) {
//无效索引,返回-1
if(index < 0 || index >= size) {
return -1;
}
//寻找节点值
ListNode cur = head;
if (index + 1 < size - index) {
for(int i = 0; i < index + 1; ++i) {
cur = cur.next;
}
} else {
cur = tail;
for(int i = 0; i < size - index; ++i) {
cur = cur.pre;
}
}
return cur.val;
}
public void addAtHead(int val) {
ListNode pre = head;
ListNode after = head.next;
size++;
//添加节点
ListNode toAdd = new ListNode(val);
toAdd.next = after;
toAdd.pre = pre;
pre.next = toAdd;
after.pre = toAdd;
}
public void addAtTail(int val) {
ListNode pre = tail.pre;
ListNode after = tail;
size++;
//添加节点
ListNode toAdd = new ListNode(val);
toAdd.next = after;
toAdd.pre = pre;
pre.next = toAdd;
after.pre = toAdd;
}
public void addAtIndex(int index, int val) {
//添加节点位置 大于 链表索引
if(index > size) {
return;
}
//添加索引 小于 0, 那就给它算作是0
if(index < 0) {
index = 0;
}
//寻找要添加索引的前一个节点 和 后一个节点
ListNode pre, after;
if(index < size - index) {
pre = head;
for(int i = 0; i < index; i++) {
pre = pre.next;
}
after = pre.next;
} else {
after = tail;
for(int i = 0; i < size - index; i++) {
after = after.pre;
}
pre = after.pre;
}
size++;
//添加节点
ListNode toAdd = new ListNode(val);
toAdd.next = after;
toAdd.pre = pre;
pre.next = toAdd;
after.pre = toAdd;
}
public void deleteAtIndex(int index) {
//无效索引
if(index < 0 || index >= size) {
return;
}
//寻找要删除索引的前一个节点 和 后一个节点
ListNode pre, after;
if(index < size - index) {
pre = head;
for(int i = 0; i < index; i++) {
pre = pre.next;
}
after = pre.next.next;
} else {
after = tail;
//size - index - 1为啥?你得找到所要删除节点的前一个节点的索引,那必须减一
for(int i = 0; i < size - index - 1; i++) {
after = after.pre;
}
pre = after.pre.pre;
}
size--;
//删除操作
pre.next = after;
after.pre = pre;
}
}
/** * Your MyLinkedList object will be instantiated and called as such: * MyLinkedList obj = new MyLinkedList(); * int param_1 = obj.get(index); * obj.addAtHead(val); * obj.addAtTail(val); * obj.addAtIndex(index,val); * obj.deleteAtIndex(index); */
双向链表也可以不用这么写,不考虑是从前还是从后开始查,这样会更简单一些,哈哈!那这样的话,代码比我这样写要简单很多了,只需要考虑从前往后查就可以!
版权声明
本文为[AI很不错呦]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40549426/article/details/124301388
边栏推荐
- 大量mapper IO优化(使用多线程异步+CountDownLatch)
- Redis's key and value best practices
- I neglected to prepare for this interview, which made me beat the day before yesterday
- 华中师范大学少年儿童组织与思想意识教育考研上岸前辈备考经验
- Matlab learning notes - calculate eigenvectors and manually execute PCA
- What are the types of documenter examination questions? How to prepare for the documenter examination of the construction department
- @Requestmapping get request parameters
- Dialogue: Shen Si L, founder of papaya mobile, from Silicon Valley to Beijing
- 2022年电工(初级)考试题库及在线模拟考试
- SDF accelerate trace
猜你喜欢

On "waiting for awakening mechanism"

CDH6. 3.2 enable Kerberos authentication
![[state machine] 388 The longest absolute path of the file](/img/58/9ffc078c4c2877307f6c2b09c1fd31.jpg)
[state machine] 388 The longest absolute path of the file

7-1 C language programming experiment 6-3 insertion of one-way linked list (30 points)

面试官宁愿要刚刚毕业工作1年的我小弟,也不要工作5年的我,年薪25w

基于PAOGD_HW1的弹出的小球-简单建模、插值动画

Use of list
十月的Android面试之旅,惨败在字节三面,幸斩获小米Offer

Virtual machine building and installation pulsar environment tutorial (for development and testing)

Basic use and principle of Minio
随机推荐
Huawei machine test question -- hj72 100 money to buy 100 chickens
[suggestions collection] no highlights in the interview
二、线性回归
Your so-called comfort is slowly destroying you!
Building a new generation of computing platform, stepvr will open the "door" of metauniverse in 2022
Huawei computer test question - hj61 put apple
编程实用工具,总有一个你用的上
Error running ‘JeecgSystemApplication‘: Command line is too long. Shorten command line for JeecgSyst
MySQL 第8章 数据库约束
LeeCode 130. Surrounded area
博途PLC用户自定义数据类型
2023年华中科技大学自动化考研上岸前辈备考经验指导
华中师范大学少年儿童组织与思想意识教育考研上岸前辈备考经验
基于OpenGL的贪吃蛇游戏设计与实现
【数据库学习01】
UnityShader入门精要——素描效果渲染
Basic use and principle of Minio
中美程序员对比:你认同吗
buuctf-[Flask]SSTI
[state machine] 388 The longest absolute path of the file