Singly linked list and its reversal

The original text was first published on WeChat public account: Jongxingzhi (jzman-blog)

A singly linked list is a chained access data structure that uses a set of storage units with arbitrary addresses to store the data elements in the linear list. The data in the linked list is represented by nodes. Each node is composed of elements and pointers. The elements are the storage units for storing data. The pointers are the address data connecting each node. This article will introduce what is a singly linked list and a single The main contents of the flip of the linked list are as follows:

  1. What is a singly linked list
  2. Traverse the reverse singly linked list
  3. Recursively reverse singly linked lists

What is a singly linked list

For each node of a singly linked list, there are two storage areas, one to store the data of the corresponding node, and the other to store the address of the next node of the node, which can be called the next pointer (next), the diagram of the singly linked list as follows:

  • Head node: the first node of the linked list, used to record the base address of the linked list

  • End node: the last node of the linked list, and its successor pointer (next) points to an empty address

Traverse the reverse singly linked list

Remember, the inversion of the linked list is actually to point the pointer in the linked list in the opposite direction. Assuming that the singly linked list is A->B->C->D, after the reversal, it will be D->C->B->A. Reversing a singly linked list by traversal means reversing each node in turn.

When reversing sequentially, the next pointer of node A points to null, and then the next pointer of node B points to node A. Finally, the traversal completes the reversal of the singly-linked list. The diagram of the traversal reversed singly-linked list is as follows:

The specific code is as follows:

/**
 * 链表反转(遍历)
 *
 * @param head
 * @return
 */
public Node<T> reverse(Node<T> head) {
    
    

    /**
     * 思路:对于链表A->B->C->D将其反转就是将头结点也就是A结点指向null,在将B结点指向A结点,依稀类推
     *      反转过程中要注意链表的断开,防止丢失断开的链表
     *
     * 链表反转实际上就是将链表中的指针指向相反方向
     */

    Node<T> tempNode = null;
    while (head != null) {
    
    
        // 记录因为反转断开的链表
        Node<T> nextNode = head.next;
        // 链表反转
        head.next = tempNode;
        // 移动tempNode指针
        tempNode = head;
        // 移动head指针
        head = nextNode;
    }
    return tempNode;
}

Recursively reverse singly linked lists

The second way to reverse a singly-linked list is recursion. The core idea is to turn big problems into smaller ones. Generally speaking, singly-linked lists with multiple nodes can be further divided into smaller singly-linked lists, such as a single-linked list of one node. The linked list is itself, the two-node single-linked list A->B->null, when inverted, the pointer of the adjacent node can be reversed, that is, the next pointer of node B points to A, and then the next pointer of A is still Point to B, so you need to point A's next pointer to null. The code is as follows:

// 链表A->B->null反转
B.next = A;
A.next = null;

The recursive method of reversing the singly linked list is as follows:

The specific code is as follows:

/**
 * 链表反转(递归)
 *
 * @param head
 * @return
 */
public Node<T> reverse(Node<T> head) {
    
    

    /**
     * 思路:使用递归思想解决
     *
     * 链表反转始终要记得两个相邻结点的指针反向
     */

    if (head == null || head.next == null) {
    
    
        return head;
    }
    
    // newNode即最终反转后的头结点
    Node<T> newNode = reverse1(head.next);
    head.next.next = head;
    head.next = null;
    return newNode;
}

The two ways of singly linked list and its reversal are introduced above. Please correct me if there is any error.

Guess you like

Origin blog.csdn.net/jzman/article/details/108743910