当前位置:网站首页>Define linked list (linked list)
Define linked list (linked list)
2022-04-23 10:15:00 【Popuessing's Jersey】
Definition int Type linked list
public class ListNode {
int val; // data : Node data
ListNode next; // object : Reference the next node object ,
// Add construction method
ListNode(int val){// Construction method : The construction method is the same as the class name
this.val = val; // Assign the received parameter to the current class val Variable
}
}
Define generic linked list
public class ListNode<E> {
E val; // data : Node data
ListNode next; // object : Reference the next node object ,
// Add construction method
ListNode(E val){// Construction method : The construction method is the same as the class name
this.val = val; // Assign the received parameter to the current class val Variable
}
}
Create linked lists and traverse linked lists
static class Test_{
// Static declarations are not allowed in inner classes
public static void main(String[] args) {
// Create the first node
ListNode nodeSta = new ListNode(0);
// Declare a variable to point to the current node during the move
ListNode nextNode;
// Point to the head node
nextNode = nodeSta;
// Create a linked list ( The length is 10)
for (int i = 1; i <10 ; i++) {
// Generate new nodes
ListNode node = new ListNode(i);
// Connect the new node
nextNode.next = node;
// Move the current node back
nextNode = nextNode.next;
}// When the cycle is complete ,nextNode Point to the last node
// Reassign the value so that the mobile node points to the first node
nextNode = nodeSta;
// Printout
print(nextNode);
}
static void print(ListNode listNode){
// Create a linked list node
while (listNode!=null){
if(listNode.next==null){
System.out.print(listNode.val);
}else {
System.out.print(listNode.val + "->");
}
listNode = listNode.next;
}
}
}
Output results :0->1->2->3->4->5->6->7->8->9
Insert the list
// Insert node
while(nextNode!=null){
// This is because of the linked list defined by the model used before , Therefore, it is necessary to specify val The data type of is int
if((int)nextNode.val==5){
ListNode newnode = new ListNode(99);
// Save the next node
ListNode node = nextNode.next;
// Insert new node
nextNode.next = newnode;
// The next node of the new node points to the previously saved node
newnode.next = node;
}
nextNode=nextNode.next;
}// After the cycle ,nextNode Point to the first node
nextNode =nodeSta;
print(nextNode);
}
// Printout
static void print(ListNode listNode){
// Create a linked list node
while (listNode!=null){
if(listNode.next==null){
System.out.print(listNode.val);
}else {
System.out.print(listNode.val + "->");
}
listNode = listNode.next;
}
}
Output results : 0->1->2->3->4->5->99->6->7->8->9
Replacement node
// Replacement node
while(nextNode!=null){
if ((int)nextNode.val==4){
// Generate new nodes
ListNode newnode = new ListNode(98);
// Save the next node of the replacement node
ListNode node = nextNode.next.next;
// Point the replacement node to null, Waiting for garbage collection
nextNode.next.next = null;
// Point the current node to the new node
nextNode.next = newnode;
// Point the node to the next node of the original replacement node
newnode.next = node;
}
// Move current node
nextNode = nextNode.next;
}
Output results :0->1->2->3->4->98->99->6->7->8->9
Delete node
// Delete node
while(nextNode!=null){
if ((int)nextNode.val==3){
// Keep the pointing node of the next node of the current node
ListNode node = nextNode.next.next;
// Point the next node of the current node to null, Waiting for garbage collection
nextNode.next.next = null;
// Point the current node to the reserved node
nextNode.next = node;
}
// The mobile node moves backward
nextNode = nextNode.next;
}
Output results :0->1->2->3->98->99->6->7->8->9
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011141196.html
边栏推荐
猜你喜欢
MapReduce计算流程详解

Yarn核心参数配置

【无标题】

Read LSTM (long short term memory)

《谷雨系列》空投

JUC concurrent programming 09 -- source code analysis of condition implementation

Sim Api User Guide(6)

Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022

Sim Api User Guide(6)

【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
随机推荐
一文看懂 LSTM(Long Short-Term Memory)
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
Realizing data value through streaming data integration (5) - stream processing
CSP certification 202203-2 travel plan (multiple solutions)
PHP two-dimensional array specifies that the elements are added after they are equal, otherwise new
LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
Sim Api User Guide(8)
2022 mobile crane driver test question bank simulation test platform operation
Can Jerry's AES 256bit [chapter]
【无标题】
杰理之通常影响CPU性能测试结果的因素有:【篇】
MapReduce计算流程详解
101. Symmetric Tree
Solve the problem of installing VMware after uninstalling
使用IDEA开发Spark程序
MapReduce core and foundation demo
lnmp的配置
202、快乐数