Algorithm Clearance Village—Finally learned how to reverse linked lists 2

Directory of articles in the Algorithm Clearance Village series



Preface

本系列文章是针对于鱼皮知识星球——编程导航中的算法通关村中的算法进行归纳和总结。 该篇文章讲解的是第二关中的青铜挑战———手写链表反转

Linked list inversion is nothing more than reversing the direction of each node. The core consideration is nothing more than one

After the reverse, how to deal with other nodes originally connected to the node?

Classic linked list reversal methods include head insertion and direct reversal.


1. Direct reversal method

The direct inversion method actually reverses the direction of each node.

  1. cur – points to the node being processed
  2. pre–points to the first node of the reversed linked list, which is also the previous node that completed the reversal.
  3. temp – points to the next node to be reversed

During the reversal process of each node, the operations required

  1. Use temp to retain nodes linked by cur
  2. Let cur's next point to the prev node
  3. Assign the cur node to the pre node
  4. Assign the temp node to the cur node

  1. First lettemp retain the node linked by cur because when the final reversal is made, the next of cur is pointed to prev. If not saved in advance, pointing directly will cause the node linked by cur to be lost.
  2. The order of steps 3 and 4 above cannot be reversed. If the temp node is assigned to the cur node first, then the cur node will no longer point to the node that has just completed the inversion, but to the next one. Inverted node, this node is not linked to the inverted linked list.

Insert image description here
Convert to code

In fact, it is reversed directly from the second node, so here you need to set head.next=null; otherwise, the head will still be linked to the second node later, forming a ring.

        ListNode current=head.next;
        ListNode pre=head;
        head.next=null;
        while (current!=null){
    
    
            // 将要处理的节点后面的链表存起来
            ListNode temp=current.next;
            // 还是将要处理的节点连接到反转的链表的头部
            current.next=pre;
            pre=current;
            current=temp;
        }
        return pre;


2. Establish virtual head node to assist inversion-----Head insertion method

In the direct reversal method, we directly reverse the original linked list.
The head insertion method is to create a new linked list, and then remove the nodes of the original linked list one by one and put them into the new linked list

  1. cur—consistent with cur in the direct insertion method, still represents the node being processed
  2. ans—virtual head node, the value it carries is meaningless and is only used to process the reversal of the node.

During the reversal of the head insertion method

next of ans always points to the first node of the reversed linked list

  1. Use temp to retain nodes linked by cur
  2. Point cur's next to the node pointed to by ans' next
  3. Point the next of ans to the current cur node
  4. Assign the temp node to the cur node
    Insert image description here
  1. Because ans.next always points to the first node of the reversed linked list, pointing cur's next to the node pointed by ans' next is to extract the node from the original linked list and add it to the head of the reversed linked list.
  2. Pointing next of ans to the current cur node is to make ans point to the head of the reversed list again.

Convert to code

        // 虚拟头节点 这里的-1就代表 这个节点携带的信息是不需考虑的
        ListNode virtual=new ListNode(-1);
        virtual.next=head;
        // 当前节点
        ListNode current = head.next;
        // 反转后 将首节点后面的连接取消掉
        head.next=null;
        ListNode temp = null;
        while (current != null) {
    
    
            // 先把要处理节点的后一个节点存起来
            temp = current.next;
            // 将要处理节点连接到反转链表的头部
            current.next = virtual.next;
            // 虚拟头节点连接到最新的反转链表的头节点
            virtual.next = current;
            // current节点往后移动
            current = temp;
        }

        return virtual.next;

Summarize

After understanding the two methods, we will find that the ideas of the two methods are actually the same. They both save the next node of the cur node first, and then link
and then Head node or pre and then reset again. The simple summary is Save resources, reverse links, reset state
In fact, there are more than these two methods of reversing linked lists, there are also recursive methods. We will explain these methods later in the game.

Guess you like

Origin blog.csdn.net/aaaaaaaa273216/article/details/133147210