Java single linked list reversal (head insertion method) detailed explanation

Linked list reversal ideas (brief description):

        Linked lists are divided into single linked lists and double linked lists, subdivided into one-way circular linked lists and bidirectional circular linked lists. This chapter mainly talks about the inversion of singly linked list. Before coming into contact with this article, I believe that everyone has already understood the linked list (a part of the linked list). The linked list is composed of nodes, and each node contains a data field and a pointer field . The data field is used to store data, and the pointer field is used to store data. Store the memory address to point to the next node.

        When the linked list is reversed, the pointer of each node must point to the previous node.

        The so-called reversal of the linked list is simply to reverse the pointer of the node. For example, there are 4 reference variables now, next (pointer to the next node), pre (predecessor of the node), newhead (current node), nexthead (successor ), when reversing, we need to temporarily store the pointer of each node pointing to the next node, because if the linked list is reversed first, the subsequent nodes will be disconnected from the previous nodes, so we need to store this pointer so that The backward movement of the last pointer. Next we use code to illustrate:

public class Linode<E> { //Create node class 
    E data; //Create data field 
    Linode<E> next; //Pointer field 

    public Linode(E data) { //Data field assignment 
        this.data = data; 
    } 
    public boolean add(E data){ //The method of adding elements 
        Linode<E> newnode = new Linode<>(data); //Create a new node and pass in data 
        if(this.next == null){ 
            this.next = newnode; //The pointer to write the next node points to the new node 
        }else{ 
            this.next.add(data); //The pointer moves backwards and calls the add method to add the element 
        } 
        return true; 
    } 
    public void print() { //Output linked list 
        System.out.println(this.data); //Output the data of the pointer field
        if(this.next != null){  
            System.out.println("-->");
            this.next.print(); //Recursive call itself 
        } 
    } 
    public Linode<E> reverlist(Linode<E> head){ //Reverse method, pass a head node 
        Linode <E> pre = null; //The predecessor of the linked list 
        Linode<E> newhead = head; //The current node 
        Linode<E> next1 = null; //Successor (stores the address pointing to the next node) 
        while (newhead != null) { //When the current node is not empty 
            next1 = newhead.next; //Store the address pointing to the next node 
            newhead.next = pre; //The pointer of the current node points to the predecessor 
            pre = newhead; //The predecessor pointer moves backward 
            newhead = next1; //The pointer of the current node moves backward 
        } 
        return pre;//return to predecessor 
    }

    public static void main(String[] args) { 
        Linode<Integer> lin = new Linode<>(1); //Constructor initializes the first node 
        lin.add(2); 
        lin.add(3); 
        lin. add(4); 
        lin.add(5); 
        lin.add(6); 
        System.out.println("before:"); 
        lin.print(); 
        System.out.println("after:") 
        ; .out.println(lin.reverlist(lin.next).data); 
    } 
}

 The result of running the above code is shown in the figure below:

 Finally, a head node is returned, and there are other methods for reversing the linked list, such as: stack method, three-pointer method, and recursive method. This chapter will not introduce it.

Guess you like

Origin blog.csdn.net/qq_62414755/article/details/125005848