当前位置:网站首页>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
边栏推荐
- 997、有序数组的平方(数组)
- Configuration of LNMP
- Jerry's users how to handle events in the simplest way [chapter]
- Depth selector
- Solve the problem of installing VMware after uninstalling
- "Gu Yu series" airdrop
- 2022茶艺师(初级)考试试题模拟考试平台操作
- LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
- IDEA——》每次启动都会Indexing或 scanning files to index
- Ansible cloud computing automation command line compact version
猜你喜欢
《谷雨系列》空投
Read LSTM (long short term memory)
JVM——》常用命令
Operation of 2022 tea artist (primary) test question simulation test platform
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
lnmp的配置
Custom login failure handling
Sim Api User Guide(4)
JUC concurrent programming 09 -- source code analysis of condition implementation
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
随机推荐
一文读懂PlatoFarm新经济模型以及生态进展
杰理之更准确地确定异常地址【篇】
Zhengda international explains what the Dow Jones industrial index is?
DBA常用SQL语句(4)- Top SQL
SQL调优系列文章之—SQL调优简介
LeetCode-608. 树节点
Sim Api User Guide(5)
1、两数之和(哈希表)
242、有效字母异位词(哈希表)
What about Jerry's stack overflow? [chapter]
CSP certification 202203-2 travel plan (multiple solutions)
LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
206、反转链表(链表)
C语言——自定义类型
JVM——》常用命令
What are the system events of Jerry's [chapter]
The central control learning infrared remote control module supports network and serial port control
Chapter I Oracle database in memory related concepts (Continued) (im-1.2)
Understand the new economic model of platofarm and its ecological progress
DBA常用SQL语句(2)— SGA和PGA