I understand how linked lists work but this particular code is tough to grasp for me.
Its this leetcode problem (basically we are given address of a node which is to be deleted) whose solution can be implemented like the code snippet below:
1. class Solution {
2. public:
3. void deleteNode(ListNode* node) {
4. ListNode* next = node->next;
5. *node = *next;
6. delete next;
7. }
8. };
I know that:
&node
would mean the address of node variablenode
means the value (information) stored at address called node*node
is used to dereference the pointer variable called node.
My doubt:
- [IMP] If we need to dereference a
node
pointer to get its data (like in line 5), then why not do so while accessing its member elements too (in line 4node->next
)? - [Not IMP] Then, how does
*node
copies*next
's data?