当前位置:网站首页>LeetCode - 707 设计链表 (设计)
LeetCode - 707 设计链表 (设计)
2022-07-25 15:32:00 【三岁就很萌@D】


单链表

注意 第一次添加节点 或者 addAtIndex(添加的节点在末尾)deleteAtIndex(删除了末尾的节点) 都有可能造成尾指针的变动 要进行判断并且更新尾指针
class MyLinkedList {
class Node{
int val;//节点的值
Node next;//节点的next指针
public Node(int val){
this.val = val;
}
}
private Node head;//头节点
private Node tail;//尾节点
public MyLinkedList() {
head = new Node(0);//建立头节点 头节点不存储值 第一个值存储在头节点的next中
tail = head;//尾指针指向头节点
}
public int get(int index) {
// i 和 node要对应 i = 0 正好是head.next
//头节点不存储值 第一个值存储在头节点的next中
int i = 0;
Node node = head.next;
//遍历寻找index对应节点
while(i < index && node != null){
node = node.next;
i++;
}
//链表中存在index对应的节点
if(node != null)//返回其值
return node.val;
else
return -1;
}
public void addAtHead(int val) {
//新建节点
Node node = new Node(val);
//将该节点插入头节点和头节点.next之间
node.next = head.next;
head.next = node;
//如果当前链表中只有一个节点
//将尾指针指向该节点
if(node.next == null)
tail = node;
}
public void addAtTail(int val) {
//新建节点
Node node = new Node(val);
//将其添加在尾节点之后
tail.next = node;
//此时该节点成为末尾节点 将尾指针指向它
tail = node;
}
public void addAtIndex(int index, int val) {
//寻找第index-1个节点
int i = -1;
Node node = head;
while(i < index-1 && node != null){
i++;
node = node.next;
}
//如果存在第index-1个节点
if(node != null)
{
//新建节点
Node addNode = new Node(val);
addNode.next = node.next;
node.next = addNode;
//注意可能添加到末尾更新tail
if(addNode.next == null)
tail = addNode;
}
}
public void deleteAtIndex(int index) {
//寻找第index-1个节点
int i = -1;
Node node = head;
while(i < index-1 && node != null){
i++;
node = node.next;
}
//存在第index-1个节点
//并且其后还有第index个节点
if(node != null && node.next!=null)
{
//删除第index个节点
node.next = node.next.next;
//可能删掉了最后一个节点
if(node.next == null){
tail = node;
}
}
}
}
/** * 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); */
边栏推荐
- Binary complement
- Pat grade a 1151 LCA in a binary tree (30 points)
- MySQL优化总结二
- 理解“平均负载”
- Games101 review: Transformation
- <栈模拟递归>
- window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
- Use cpolar to build a business website (how to buy a domain name)
- pageHelper不生效,sql没有自动加上limit
- C#精挑整理知识要点12 异常处理(建议收藏)
猜你喜欢

Games101 review: 3D transformation

MySQL transactions and mvcc

JVM—类加载器和双亲委派模型

带你创建你的第一个C#程序(建议收藏)

CF888G-巧妙字典树+暴力分治(异或最小生成树)

Use cpolar to build a business website (how to buy a domain name)

window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28

图论及概念

Cf888g clever dictionary tree + violent divide and conquer (XOR minimum spanning tree)

MATLAB 如何生产随机复序列
随机推荐
ML - 自然语言处理 - 自然语言处理简介
pageHelper不生效,sql没有自动加上limit
C # fine sorting knowledge points 9 Set 2 (recommended Collection)
2021 Shanghai match-b-ranked DP
ML - 语音 - 语音处理介绍
ML - Speech - advanced speech model
Binary complement
CF365-E - Mishka and Divisors,数论+dp
盒子躲避鼠标
分布式原理 - 什么是分布式系统
No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
Pytorch学习笔记--常用函数总结2
ML - natural language processing - Basics
Cf566a greed + dictionary tree
ML - 图像 - 深度学习和卷积神经网络
解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题
Qtime definition (manual waste utilization is simple and beautiful)
4PAM在高斯信道与瑞利信道下的基带仿真系统实验
LeetCode - 622 设计循环队列 (设计)
哪里有搭建flink cdc抽mysql数的demo?