Introduction to algorithm C-206. Reverse linked list

Keywords: C Algorithm linked list

LeetCode problem brushing - algorithm learning plan (Introduction)

Title Description

Introduction of ideas

If the linked list is bidirectional, reversing the linked list is very simple. You only need to exchange the forward pointer and backward pointer. But now the topic specifies a single linked list, how to reverse it? In fact, it is also to flip the direction of the backward pointer, such as changing 1 - > 2 - > 3 - > null to 3 - > 2 - > 1 - > null, but the direction of the linked list pointer has changed.

We can define a linked list pointer pre to point to the previous node of the current linked list node (cur to the current node), and then define a linked list pointer next to point to the next node of cur.
At the beginning, pre points to NULL, cur points to the header node, and next points to the next node of the header node; Then enter a cycle (the cycle condition is that cur is not empty). Each time, let the next point to cur - > next (temporarily store the next node); Then point cur - > next to pre. This step is exactly what we said earlier to flip the back of the linked list to the direction of the pointer; After reversing the linked list pointer, we also need to consider the next cycle, so we need to point pre to cur and cur to next, so the next cycle operation is the next node that is not reversed; When cur is empty, it means that it reaches the tail of the original linked list. At this time, pre points to the inverted head node.

My first correct submission

Although the topic is very simple, I still make mistakes many times. Even if I succeed in the last time, there are still many places to optimize

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* reverseList(struct ListNode* head)
{
    struct ListNode *pre = NULL;
    struct ListNode *cur = head;
    struct ListNode *next  = NULL;
    while(cur)
    {
        next  = cur->next;
        cur->next = pre;
        pre = cur;
        cur = next ;
    }
    return pre;
}

Official version

Method 1: iteration

Assuming that the linked list is 1 → 2 → 3 →∅, we want to change it to ∅← 1 ← 2 ← 3.
When traversing the linked list, change the next pointer of the current node to point to the previous node. Since a node does not reference its previous node, its previous node must be stored in advance. The latter node also needs to be stored before changing the reference. Finally, a new header reference is returned.
——Author: leetcode solution—— link ——Source: LeetCode

struct ListNode* reverseList(struct ListNode* head) {
    struct ListNode* prev = NULL;
    struct ListNode* curr = head;
    while (curr) {
        struct ListNode* next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

Author: LeetCode-Solution
 Link: https://leetcode-cn.com/problems/reverse-linked-list/solution/fan-zhuan-lian-biao-by-leetcode-solution-d1k2/
Source: force buckle( LeetCode)
The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

Complexity analysis
Time complexity: O(n), where n is the length of the linked list. You need to traverse the linked list once.
Space complexity: O(1).

Method 2: recursion

The recursive version is a little more complicated, and the key is to reverse work. Assuming that the rest of the linked list has been reversed, how should we reverse the previous part now?
If you are interested, you can check the official explanation

struct ListNode* reverseList(struct ListNode* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    struct ListNode* newHead = reverseList(head->next);
    head->next->next = head;
    head->next = NULL;
    return newHead;
}

Author: LeetCode-Solution
 Link: https://leetcode-cn.com/problems/reverse-linked-list/solution/fan-zhuan-lian-biao-by-leetcode-solution-d1k2/
Source: force buckle( LeetCode)
The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

Complexity analysis
Time complexity: O(n), where n is the length of the linked list. It is necessary to reverse each node of the linked list.
Space complexity: O(n), where n is the length of the linked list. The space complexity mainly depends on the stack space of recursive calls, up to N layers.

Posted by lordshoa on Wed, 10 Nov 2021 09:14:53 -0800