当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
454、四数之和(哈希表)
《Redis设计与实现》
Realizing data value through streaming data integration (5) - flow analysis
实践六 Windows操作系统安全攻防
C language: expression evaluation (integer promotion, arithmetic conversion...)
转:毛姆:阅读是一座随身携带的避难所
Chapter 2 Oracle database in memory architecture (I) (im-2.1)
使用IDEA开发Spark程序
Juc并发编程06——深入剖析队列同步器AQS源码
杰理之栈溢出 stackoverflow 怎么办?【篇】
【无标题】
第一章 Oracle Database In-Memory 相关概念(IM-1.1)
Go language practice mode - functional options pattern
杰理之通常影响CPU性能测试结果的因素有:【篇】
What about Jerry's stack overflow? [chapter]
链表相交(链表)
ansible playbook语法和格式 自动化云计算
Common SQL statements of DBA (6) - daily management
Pyqt5 and communication
Ansible cloud computing automation command line compact version








