Children, do you have many question marks? Why? Others are reading comics, while I am learning to draw and speak to the piano
1, Single linked list introduction and memory layout
A linked list is an ordered list. Its actual storage structure in memory is as follows:
Although it seems to be out of order, ta depends on the address and next field of each link list node element to distinguish the order of end-to-end connection. As shown in the figure below, from the head pointer to the first element, then the second and third
Logical structure of linked list:
2, Single chain table creation, traversal implementation and single chain table node addition, deletion, modification and query operations
1. Create, add, traverse display
The model is as follows: 1) head node 2) middle node 3) tail node
The next field of each node points to the object address of the next node, and the end node is empty
To create a new entity class:
package ...; /** * @Author: ldk * @Date: 2020/3/29 21:18 * @Describe: */ public class HeroNode { public int no; public String name; public String nickName; public HeroNode next;//Point to next node //Construct node object public HeroNode(int no, String name, String nickName) { this.no = no; this.name = name; this.nickName = nickName; } //Rewrite toString() @Override public String toString() { return "HeroNode{" + "no=" + no + ", name='" + name + '\'' + ", nickName='" + nickName + '\'' + '}'; }
//GetSet method self brain
}
Create SingleLinkedList class, write methods of adding and traversing, and create main method test:
package ...; /** * @Author: ldk * @Date: 2020/3/29 21:25 * @Describe: */ public class LinkedListTest { public static void main(String[] args) { HeroNode h1 = new HeroNode(1, "Zhang San", "the other woman"); HeroNode h2 = new HeroNode(2, "Li Si", "Little four"); HeroNode h3 = new HeroNode(3, "Wang Wu", "Xiao Wu"); HeroNode h4 = new HeroNode(4, "Zhao Liu", "Xiao Liu"); SingleLinkedList singleLinkedList = new SingleLinkedList(); singleLinkedList.add(h1); singleLinkedList.add(h2); singleLinkedList.add(h3); singleLinkedList.add(h4); singleLinkedList.detail(); } static class SingleLinkedList { //Create head node private HeroNode head = new HeroNode(0, "", ""); //Node adding method public void add(HeroNode heroNode) { //Get the head node HeroNode temp = head; //Find the last node, and next Domain points to the node to add while (true) { if (temp.next == null) { break; } temp = temp.next; } temp.next = heroNode; } //Display link list [traversal] public void detail() { if (head.next == null) { System.out.println("The list is empty!"); return; } //Get a temporary pointer HeroNode temp = head.next; while (true) { if (temp.next == null){ System.out.println(temp.toString()); System.out.println("That's all~~"); break; } System.out.println(temp.toString()); temp = temp.next; } } } }
Operation result:
2. Insert nodes in order
The above test is to insert from 1, 2, 3 and 4 in turn. Then, the linked list itself is in order. Can we insert in disorder according to the no field to achieve automatic incremental sorting, and the repeated elements will not be inserted again,
This means that the insertion order is changed to 1432, but the internal structure of the list is still 1234
The code only needs to modify the add() method slightly:
//According to field no Ascending insertion public void add2(HeroNode heroNode) { //Get pointer HeroNode temp = head; while (true) { if (temp.next == null) { break; } else if (temp.next.no == heroNode.no) { System.out.println("The node already exists~"); return; } else if (temp.next.no > heroNode.no) { break; } temp = temp.next; } heroNode.next = temp.next; temp.next = heroNode; }
Messy and repetitive insertion order:
Ordered print results:
It's still in 1234 order, and repeated elements are no longer inserted~
Other additions, deletions, modifications and searches can be done by yourself when you have time. I won't repeat them here.
3, Interview questions of sina, Tencent and Baidu
Answer the idea of temporary value analysis, and fill in the code later:
1) Idea: start from the next node and traverse all the time. Each traversal is + 1 until. Next is empty. The result is the length of the linked list
2) Idea: it is still traversal, from the first to the next of the length-k is K
Code (to be supplemented):
3) Train of thought: a little difficult, but also very clear.
It can be divided into master chain and sub chain. The master chain has two pointers and the sub chain has one pointer
The parent chain splits a node and splices it to the first one after the head of the sub chain, and so on. The new sub chain obtained is the reverse chain:
Code (to be supplemented):
4) Idea: the same as 4. After reversing, print.
5) Idea: take one chain list as the parent chain and the other as the child chain, traverse the child chain and insert the parent chain one by one.
2020-03-29 21:02:13