2020-8-03 Daily Questioning Notes-Reverse Linked List Ⅰ

Daily reading notes-reversal linked list Ⅰ

On Monday, August 3, 2020, halfway through the summer vacation of the freshman year, I was interested in self-study data structure and reproduced some basic operations, but I still felt that I saw very little, so I decided to start brushing Leetcode and brushing the questions for a few days ( After reading the answers for a few days), I also read a lot of related problem-solving guide articles, but I still feel that the impression is not deep enough, so I decided to start writing notes on the problem, brainstorming, and write down my own ideas for solving the problem. !

Disclaimer: The article refers to the online problem-solving guide article, which is only used for deepening and consolidation, learning and communication. My level is limited. If there are errors in the article, please criticize and correct. The non-commercial reprinted pictures have all indicated the source. If there is infringement, please Private chat contact deletion.
Insert picture description here
Method 1:
Double pointer iteration method: initialize two pointers pre and cur, and each iteration makes the current node pointer (cur) point to the previous node (pre)
(remember to save the next node of the current node in advance ) Location)
Picture source
link①C++ implementation

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* reverseList(ListNode* head) {
    
    
        ListNode* pre = NULL;
        ListNode* cur = head;
        while(cur)
        {
    
    
            ListNode* temp = cur->next;// 提前保存好当前节点的下一节点
            cur->next = pre;           // 当前节点的指针指向前驱节点
            pre = cur;                 //更新前驱节点
            cur = temp;                //cur指向原来的下一节点
        }
        return pre;
    }
};

②Python implementation

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        pre = None
        cur = head
        while cur:
            temp = cur.next   # 提前保存好当前节点的下一位
            cur.next = pre    # 当前节点的指针指向前驱节点
            pre = cur         #更新pre
            cur = temp        # cur指向原来的下一位
        return pre

Python another way - multi-writing assignment : to use its properties can be omitted to save a pointer to the current node's next steps , very cleverly written, interested can take a look below this article, no further explanation
Python yuan Assignment

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def reverseList(self, head:ListNode) -> ListNode:
        """
        :type head: ListNode
        :rtype: ListNode
        """
        pre = None
        cur = head
        while cur:
            cur.next,pre,cur= pre,cur,cur.next
        return pre

Method 2: Recursion
Call the function recursively, and recurse to the last node of the linked list, which is the head node after the reversal (denoted as ret).
Conditions for the end of recursion:

if(head == NULL || head->next == NULL) 
    return head;

After that, each time the function returns, the next pointer of the next node of the current node is made to point to the current node, and the next pointer of the current node is made to point to NULL, so as to realize the partial reversal from the end of the linked list (1 ->2 is converted to 1<-2)
When all the recursive functions are out of the stack, the linked list is reversed to complete the
picture source link
Insert picture description here
①C++ implementation

ListNode* reverseList(ListNode* head) {
    
    
	//到链表尾节点结束,返回作为反转链表的头节点
    if(head == NULL || head->next == NULL) 
        return head;
	//递归调用直到满足结束条件,返回
    ListNode* ret = reverseList(head->next);  
    //注意我们的递归调用入口参数:head->next,也就是下面代码中的head
    //第几次调用,递归调用返回后 head就指向第几个节点
    //接下来:当前节点的下一个节点的后驱指针指向当前节点(有点别扭)
    //简单的说就是 1->2 我们需要实现1<-2, 
    //head指向的是1,那么head->next就是2,2的next指向1,就实现我们的目的啦
    head->next->next = head;
    head->next = NULL;
    return ret;
}

②Python implementation, the idea is the same as the above, so I won't repeat it here

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head
        ret = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return ret

Guess you like

Origin blog.csdn.net/weixin_45336082/article/details/107776374