Reputation: 2002
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.
Upvotes: 2
Views: 308
Reputation: 97
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 :
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.
Upvotes: 2
Reputation: 2002
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 ofnode
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.
Upvotes: 1