One , Get the number of nodes in the single linked list
/* * head Head node of linked list * Returns the number of valid nodes , There is no statistical head node * */ public static int getLength(HeroNode
head) { if(head.next == null) { return 0; } int length = 0; // Define an auxiliary variable HeroNode
cur = head.next; while(cur != null) { length++; cur = cur.next; } return
length; }
Two , Look up the second part of a single linked list K Nodes
// thinking //1, Write a method to receive , receive head node , Receive one at the same time index //2,index It means the penultimate index Nodes
//3, First traverse the linked list from beginning to end , Get the total length of the linked list //4, obtain size after , We start with the first one in the list (size-index) individual
//5, Find and return the node , No null returned public static HeroNode findLastIndexNode(HeroNode head,int
index) { if(head.next == null) { return null; } // First traversal to get the length of the list ( Number of nodes ) int size
= getLength(head); // Second traversal size-index position , It's our penultimate K Nodes // Make one first index Verification of
if(index<=0||index>size) { return null; } // Define as an auxiliary variable ,for Loop to the countdown index individual HeroNode
cur = head.next; for(int i=0;i<size-index;i++) { cur = cur.next; } return cur; }
Three , Inversion of single linked list
// Inversion of single linked list public static void reverseList(HeroNode head) {
// If the linked list is empty , Or only one node , There is no need to reverse , Direct return if(head.next == null||head.next.next == null) {
return; } // Define an auxiliary pointer ( variable ), Help us traverse the original list HeroNode cur = head.next; HeroNode next =
null;// Points to the current node [cur] Next node of HeroNode reverseHead = new HeroNode(0,"","");
// Traverse the original list , And complete traversal from the beginning of the original list , Every node traversed , Take it out , Put in reverseHead The front end of while(cur != null) {
next = cur.next;// Temporarily saves the next node of the current node , Because it needs to be used later cur.next =
reverseHead.next;// take cur The next node in the list points to the front end of the new list reverseHead.next = cur;// take cur Connect to a new linked list
cur = next;// Give Way cur Point to the next node , Move back } // take head.next point reverseHead.next, Realization of single linked list inversion head.next
= reverseHead.next; }
Four , Realize reverse order printing
// Usage 2 , Realize reverse order printing , Stack public static void reversePrint(HeroNode head) { if(head.next
== null) { return; } // Create a stack Stack<HeroNode> stack = new Stack<HeroNode>();
HeroNode cur = head.next; // Push all the nodes of the linked list into the stack while(cur != null) { stack.push(cur);
cur = cur.next;// Press in the next node } // Print the nodes in the stack ,pop() while(stack.size()>0) {
System.out.println(stack.pop()); } }
Five , console output : 

Six , Merging two ordered single linked lists , The merged linked list is still in order

New ideas :
// Merging two ordered single linked lists , The merged linked list is still in order public static SingleLinkedList
listJoinList(SingleLinkedList singleLinkedList1,SingleLinkedList
singleLinkedList2) { HeroNode head1 = singleLinkedList1.getHead(); HeroNode
head2 = singleLinkedList2.getHead(); if(head1.next == null) { return
singleLinkedList2; } if(head2.next == null) { return singleLinkedList1; }
// Define an auxiliary pointer ( variable ), Traverse the first linked list HeroNode cur1 = head1.next; // Points to the next node of the first node HeroNode next1
= null; // Define an auxiliary pointer ( variable ), Traversing the second linked list HeroNode cur2 = head2.next; // Points to the next node of the second
HeroNode next2 = null; // Combined linked list SingleLinkedList joinedLinkedList = new
SingleLinkedList(); // Combined chain header HeroNode joinedHead = joinedLinkedList.getHead();
/* * [0,2,4],[1,3,5], * should be 0 Time ,1,3,5 Traverse once joinedHead --> 0,1,3,5 * should be 2 Time ,1,3,5 Go through it again
joinedHead --> * should be 4 Time ,1,3,5 Go through it again * * */ // Traverse the first linked list while(cur1 != null) { next1 =
cur1.next;// Temporarily saves the next node of the current node , Because it needs to be used later // Traversing the second linked list while(cur2 != null) { next2 =
cur2.next;// Temporarily saves the next node of the current node , Because it needs to be used later if(cur1.no>cur2.no) { cur2.next =
joinedHead.next;// take cur The next node in the list points to the front end of the new list joinedHead.next = cur2;// take cur Connect to a new linked list
}else { cur1.next = joinedHead.next;// take cur The next node in the list points to the front end of the new list joinedHead.next =
cur1;// take cur Connect to a new linked list } cur2 = next2;// Give Way cur Point to the next node , Move back } cur1 =
next1;// Give Way cur Point to the next node , Move back } return joinedLinkedList; }
  Correct code :
public static void combineList(HeroNode head1,HeroNode head2){ HeroNode next1
= head1.next; HeroNode next2 = head2.next; // Combined chain header HeroNode joinedHead = new
HeroNode(0,"",""); if(head1.next == null){ joinedHead.next = head2.next; }else
if (head2.next == null){ joinedHead.next = head1.next; } // Combined linked list
SingleLinkedList joinedLinkedList = new SingleLinkedList(); HeroNode nextJoined
= joinedHead; joinedLinkedList.head = nextJoined; while(next1 != null||next2 !=
null) { if(next1 == null && next2 != null) { nextJoined.next = next2; next2 =
next2.next; }else if(next1 != null && next2 == null) { nextJoined.next = next1;
next1 = next1.next; }else { if(next1.no <= next2.no) { nextJoined.next = next1;
next1 = next1.next; }else { nextJoined.next = next2; next2 = next2.next; } }
nextJoined = nextJoined.next; } SingleLinkedList singleLinkedList = new
SingleLinkedList(); singleLinkedList.list2(joinedHead); } // Show linked list , By traversing public
void list2(HeroNode headNode) { // First judge whether the linked list is empty if(headNode.next == null) {
System.out.println(" The linked list is empty "); return; } // Because the head node can't move , So you need an auxiliary variable to traverse HeroNode temp =
headNode.next; while(true) { // Judge whether to the end of the list if(temp == null) { break; } // Output node information
System.out.println(temp); // take next Move back , Be careful , Or it's a dead cycle temp = temp.next; } } public
static void main(String[] args) { HeroNode hero1 = new HeroNode(1," Song Jiang "," Timely rain ");
HeroNode hero2 = new HeroNode(2," Lu Junyi "," Yu Qilin "); HeroNode hero3 = new
HeroNode(3," Wu Yong "," Zhiduoxing "); HeroNode hero4 = new HeroNode(4," Gongsun Sheng "," Enter Yunlong "); // Create a linked list
SingleLinkedList singleLinkedList1 = new SingleLinkedList(); // Add by number
singleLinkedList1.addByOrder(hero1); singleLinkedList1.addByOrder(hero4);
singleLinkedList1.addByOrder(hero3); singleLinkedList1.addByOrder(hero2);
HeroNode hero5 = new HeroNode(5," Guan Sheng "," Broadsword "); HeroNode hero6 = new
HeroNode(6," Lin Chong "," Leopard head "); HeroNode hero7 = new HeroNode(7," Qin Ming "," Thunderbolt fire "); HeroNode
hero8 = new HeroNode(8," Hu yanzhuo "," Double whip "); // Create a linked list SingleLinkedList
singleLinkedList2 = new SingleLinkedList(); // Add by number
singleLinkedList2.addByOrder(hero6); singleLinkedList2.addByOrder(hero7);
singleLinkedList2.addByOrder(hero8); singleLinkedList2.addByOrder(hero5);
combineList(singleLinkedList1.getHead(),singleLinkedList2.getHead()); }
Seven , console output

Technology
©2019-2020 Toolsou All rights reserved,
Mybatis Error resolution :There is no getter for property named '*' in 'class Java.lang.String Big data tells you , How tired are Chinese women Message quality platform series | Full link troubleshooting Gude Haowen serial - You deserve to be an engineer ( Preface ) Image explanation of over fitting and under fitting Springboot of JPA Common query methods JAVA Detailed explanation of anomalies vue Of v-if And v-show The difference between python To solve the problem of dictionary writing list in Codeup——601 | problem A: task scheduling