当前位置:网站首页>Leetcode - 707 design linked list (Design)
Leetcode - 707 design linked list (Design)
2022-07-25 15:40:00 【Cute at the age of three @d】


Single chain list

Be careful Add node for the first time perhaps addAtIndex( The added node is at the end )deleteAtIndex( Deleted the end node ) May cause the tail pointer to change To judge and update the tail pointer
class MyLinkedList {
class Node{
int val;// The value of the node
Node next;// Node next The pointer
public Node(int val){
this.val = val;
}
}
private Node head;// Head node
private Node tail;// Tail node
public MyLinkedList() {
head = new Node(0);// Set up the head node The head node does not store values The first value is stored in the header node next in
tail = head;// The tail pointer points to the head node
}
public int get(int index) {
// i and node To correspond i = 0 Is precisely head.next
// The head node does not store values The first value is stored in the header node next in
int i = 0;
Node node = head.next;
// Traverse to find index Corresponding node
while(i < index && node != null){
node = node.next;
i++;
}
// There is... In the linked list index Corresponding node
if(node != null)// Return its value
return node.val;
else
return -1;
}
public void addAtHead(int val) {
// The new node
Node node = new Node(val);
// Insert this node into the head node and the head node .next Between
node.next = head.next;
head.next = node;
// If there is only one node in the current linked list
// Point the tail pointer to the node
if(node.next == null)
tail = node;
}
public void addAtTail(int val) {
// The new node
Node node = new Node(val);
// Add it after the tail node
tail.next = node;
// At this point, this node becomes the end node Point the tail pointer to it
tail = node;
}
public void addAtIndex(int index, int val) {
// Search for the first index-1 Nodes
int i = -1;
Node node = head;
while(i < index-1 && node != null){
i++;
node = node.next;
}
// If there is a second index-1 Nodes
if(node != null)
{
// The new node
Node addNode = new Node(val);
addNode.next = node.next;
node.next = addNode;
// Note that updates may be added to the end tail
if(addNode.next == null)
tail = addNode;
}
}
public void deleteAtIndex(int index) {
// Search for the first index-1 Nodes
int i = -1;
Node node = head;
while(i < index-1 && node != null){
i++;
node = node.next;
}
// There is No index-1 Nodes
// And then there is the third index Nodes
if(node != null && node.next!=null)
{
// Delete the first index Nodes
node.next = node.next.next;
// Maybe the last node was deleted
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); */
边栏推荐
- ML - natural language processing - Basics
- Deadlock gossip
- GAMES101复习:变换
- JVM - classloader and parental delegation model
- 2019浙江省赛C-错排问题,贪心
- Pytorch框架练习(基于Kaggle Titanic竞赛)
- Use cpolar to build a business website (how to buy a domain name)
- No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
- Pytorch学习笔记--常用函数总结2
- Qtime definition (manual waste utilization is simple and beautiful)
猜你喜欢

Idea eye care settings

Box avoiding mouse

MySQL - user and permission control

PAT甲级1152 Google Recruitment (20 分)

ML - natural language processing - Basics

LeetCode - 677 键值映射(设计)*

JVM知识脑图分享

LeetCode - 225 用队列实现栈

ML - natural language processing - Key Technologies

ML - natural language processing - Introduction to natural language processing
随机推荐
2021上海市赛-B-排序后dp
2019 Zhejiang race c-wrong arrangement, greedy
CF750F1-思维dp
matlab--CVX优化工具包安装
Window system black window redis error 20creating server TCP listening socket *: 6379: listen: unknown error19-07-28
Wechat applet
Understanding of this object
Qtime definition (manual waste utilization is simple and beautiful)
Hdu3873 shortest path with dependency (topological sorting)
Take you to learn more about JS basic grammar (recommended Collection)
var、let、const之间的区别
数据系统分区设计 - 分区与二级索引
Find out what happened in the process of new
CVPR 2022 | 网络中批处理归一化估计偏移的深入研究
LeetCode - 379 电话目录管理系统(设计)
Notes on inputview and inputaccessoryview of uitextfield
PageHelper does not take effect, and SQL does not automatically add limit
Cf566a greed + dictionary tree
JVM—类加载器和双亲委派模型
2019 Shaanxi provincial competition j-bit operation + greed