当前位置:网站首页>707. Design linked list (linked list)
707. Design linked list (linked list)
2022-04-23 10:15:00 【Popuessing's Jersey】
subject :
Implement these functions in the linked list class :
- get(index): Get the index Values of nodes . If the index is invalid , Then return to -1.
- addAtHead(val): Add a value of... Before the first element of the list val The node of . After inserting , The new node will be the first node in the list .
- addAtTail(val): Will be worth val To the last element of the list .
- addAtIndex(index,val): In the list, the index The value added before nodes is val The node of . If index It's equal to the length of the list , Then the node will be attached to the end of the list . If index Greater than the length of the list , The node will not be inserted . If index Less than 0, Then insert the node in the head .
- deleteAtIndex(index): If the index index It works , Delete the index Nodes .
public class Shejilianbiao {
// Define linked list
static class ListNode{
int val; // data : Node data
ListNode next; // object : Reference next data object
// Define construction method
ListNode(int val){
this.val = val;
}
}
static class MyLinkedList{
//size Stores the number of linked list elements
int size;
// Virtual header node
ListNode head;
// Initialize linked list
public MyLinkedList(){
size = 0;
head = new ListNode(0);
}
// For the first index The number of nodes
public int get(int index){
// If index illegal , return -1
if(index< 0 || index>=size){
return -1;
}
ListNode currentNode = head;
// Use for loop , Find the first (index+1) Nodes
for (int i = 0; i <=index ; i++) {
currentNode = currentNode.next;
}
return currentNode.val;
}
// Insert a node at the front of the linked list
public void addAtHead(int val){
addAtIndex(0,val);
}
// Insert a node at the end of the linked list
public void addAtTail(int val){
addAtIndex(size,val);
}
// In the index Insert a new node before two nodes , for example index by 0, Then the newly inserted node is the new head node of the linked list .
// If index It's equal to the length of the list , The newly inserted node is the tail node of the linked list
// If index Greater than the length of the list , It returns null
public void addAtIndex(int index,int val){
if(index>size){
return;
}
if (index<0){
index=0;
}// Insert node , Add one to the length of the linked list
size++;
// The precursor of the node to be inserted
ListNode pre = head;
for (int i = 0; i <index ; i++) {
pre = pre.next;
}
// Create a new node to insert
ListNode add = new ListNode(val);
// The pointer to the inserted section points to the next node of the previous node
add.next = pre.next;
// The previous node points to the insertion node
pre.next = add;
}
// Delete the first index Nodes
public void deleteAtIndex(int index){
// If index Out of range , Returns an empty
if(index < 0 || index >=size ){
return;
}
// Delete node , Total length minus one
size--;
ListNode pre = head;
for (int i = 0; i <index ; i++) {
pre = pre.next;
}
pre.next = pre.next.next;
}
}
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1,2);
int x = myLinkedList.get(1);
myLinkedList.deleteAtIndex(1);
int y = myLinkedList.get(1);
System.out.println(x+","+y);
}
}
Output results :
2,3
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011141104.html
边栏推荐
猜你喜欢
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
2022茶艺师(初级)考试试题模拟考试平台操作
Redis design and Implementation
C语言——自定义类型
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
【无标题】
Question bank and answers of Shanghai safety officer C certificate examination in 2022
一文看懂 LSTM(Long Short-Term Memory)
2022年广东省安全员A证第三批(主要负责人)考试试题及答案
深度选择器
随机推荐
通过流式数据集成实现数据价值(4)-流数据管道
Pyqt5 and communication
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
DBA common SQL statements (1) - overview information
DBA常用SQL语句(1)— 概况信息
通过流式数据集成实现数据价值(5)- 流分析
0704、ansible----01
CSP认证 202203-2 出行计划(多种解法)
Common SQL statements of DBA (6) - daily management
【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
计算机网络安全实验二|DNS协议漏洞利用实验
101. Symmetric Tree
Realizing data value through streaming data integration (4) - streaming data pipeline
2022 mobile crane driver test question bank simulation test platform operation
正大国际讲解道琼斯工业指数到底是什么?
Sim Api User Guide(5)
Formattime timestamp format conversion
深度选择器
lnmp的配置