简体   繁体   中英

C++ Reverse Linked List Recursively Why it works?

This is the problem: https://leetcode.com/problems/reverse-linked-list/

I know the solution (and it's copy-pasted all around the internet), but I don't understand how part of it works ... So:

    struct ListNode{
        int data;
        ListNode* next;
    };

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

this works.

Now I am getting it trough the debugger and see things I don't understand...

Suppose we have a linked list:

1->2->3->NULL

1 ) We have passed the last 3->Null part into ListNode* node = reverseList(head->next); and it does return head; and now ListNode* node = 3->NULL;

在此处输入图片说明

Okay, node == 3->NULL and head == 2->3->NULL

2 ) Let's step over to head->next->next = head :

在此处输入图片说明

Now head == 2->3->2->3... BUT WHY node == 3->2->3->2... ???

How are they connected? I am totally confused here.

3) on next line head -> next = NULL we can see it again affects both head and node :

在此处输入图片说明

As you see, I don't understand the connection between node and head . So I don't understand how it works and I feel I don't understand how those linked lists work in general. Maybe someone can help me? Appreciate it.

Unfortunately I do not have enough reputation to post a comment so I am going to post an answer and hope it helps you. I am going to use paint pictures to make things easier to visualize. Let's begin:

Let's say we have the list 1->2->3-> so it begins like so :

examlpe

Now we start the function and it runs until gets to the first recursion where it starts with a new head and we have the following situation:

例

Then the function runs again and we have the following outcome:

例

Then we start with a new head (3) but here the last function returns head because the if condition head->next == NULL is met. So we have:

例

Then we follow the function until the end changing the head->next->next = head and head->next = NULL so we have:

例

Then the function returns node and we return to the original call. Then we do the same steps and we end up with:

例

In the end the function returns node so we end up with:

例

Hope this helps you and sorry for the poor quality of the answer but I find it easier to solve recursion when visualizing it and the easiest way to visualize is to draw.

Okay, probably I have found an answer myself. again, the problem is here:

        ListNode* node = reverseList(head -> next);
        head -> next -> next = head;
        head -> next = NULL;
        return node;

My question was why strings 2 and 3, which change head , also affect (change) node ?

this is because node is a pointer and it 'points' to the same address in memory as head->next .

You actually can see this in the first screenshot:

在此处输入图片说明

0x100300020 is address of node

0x100300020 is address of head->next

they are pointing to the same address in memory.

So when we do:

        head -> next -> next = head;
        head -> next = NULL;

we also change it for the "thing stored" in the address where node points to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM