当前位置:网站首页>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
边栏推荐
猜你喜欢

Operation of 2022 tea artist (primary) test question simulation test platform

Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview

0704、ansible----01

JVM——》常用参数
![Jerry's more accurate determination of abnormal address [chapter]](/img/ed/a08949bfc63823baf25fd57c324996.png)
Jerry's more accurate determination of abnormal address [chapter]

2022年上海市安全员C证考试题库及答案

JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer

Yarn核心参数配置

Solve the problem of installing VMware after uninstalling

Windows安装redis并将redis设置成服务开机自启
随机推荐
Configuration of LNMP
997、有序数组的平方(数组)
DBA common SQL statements (2) - SGA and PGA
ansible 云计算 自动化
Realize data value through streaming data integration (1)
深度选择器
Yarn resource scheduler
Chapter 3 enable and adjust the size of IM column storage (im-3.1)
SQL调优系列文章之—SQL性能方法论
59、螺旋矩阵(数组)
art-template 模板引擎
第二章 In-Memory 体系结构 (IM-2.2)
Shell script interaction free
Go language practice mode - functional options pattern
LeetCode-608. Tree node
【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
Sim Api User Guide(8)
C语言——自定义类型
142、环形链表||
CSP certification 202203-2 travel plan (multiple solutions)