0

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:

  1. &node would mean the address of node variable
  2. node means the value (information) stored at address called node
  3. *node is used to dereference the pointer variable called node.

My doubt:

  1. [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 4 node->next)?
  2. [Not IMP] Then, how does *node copies *next's data?
Ensei_Tankado
  • 133
  • 2
  • 6
  • 4
    Please don't go to such so-called "competition" or "online judge" sites to learn programming or programming languages. That's not what they're for. If you want to learn C++ invest in [some good books](https://stackoverflow.com/a/388282/440558) and take proper classes. You should stay as far away from such sites as possible while learning. – Some programmer dude Oct 18 '21 at 06:25
  • _What does *node mean?_ That depends on the context. `ListNode* node` is a declaration of a function parameter. The `*` denotes a pointer type. `*node = *next;` is an expression. Now, `*` is the [indirection operator](https://en.cppreference.com/w/cpp/language/operator_member_access) (can be imagined as a kind of "contents-of-pointer operator"). `node` and `next` are pointers. `*next` delivers the contents of `next`. Actually, the returned type is a reference (or const reference). Hence, `*node` on the left hand side works as well. It provides the reference where to assign the `*next` into. – Scheff's Cat Oct 18 '21 at 06:34
  • 1
    This pointer syntax is inherited from C. K&R did this intentionally. I remember having read they considered it as nice that the `*` may appear at the same position in declaration and expressions. They probably overlooked (or ignored) the fact that learners may be simply not aware of that the `*` has nevertheless different meanings. (Not to mention, that there is also a binary operator * for multiply - as usual in other languages as well.) – Scheff's Cat Oct 18 '21 at 06:38
  • @Scheff'sCat thanks for explaining. I just forgot to mention that I knew the indirection/dereference operator. I was actually confused about my doubt no. 1 (pls check the edited question) – Ensei_Tankado Oct 18 '21 at 07:10

2 Answers2

2

node->next is actually equivalent to (*node).next. So there's an implicit dereference there already.

As for the copying, I assume you understand assignment between e.g. plain int variables? As in:

int a = 5;
int b = 10;

a = b;

It's quite natural that the value of b will be copied into a.

Now lets do the same again, but with one pointer to b:

int a = 5;
int b = 10;

int* pb = &b;  // pb is pointing to b

a = *pb;

This is really doing exactly the same as a = b.

And another example with a pointer to a instead:

int a = 5;
int b = 10;

int* pa = &a;  // pa is pointing to a

*pa = b;

Again this is the same as a = b.

Now putting them together:

int a = 5;
int b = 10;

int* pa = &a;  // pa is pointing to a
int* pb = &b;  // pb is pointing to b

*pa = *pb;

It's still the same as a = b.

It doesn't really matter if the pointers are to plain int variables or values, or to structures, it works the same for all pointers.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Your answer's first line led me to [this](https://www.usna.edu/Users/cs/wcbrown/courses/F14IC210/lec/l34/lec.html) and cleared my doubts – Ensei_Tankado Oct 18 '21 at 11:18
0

*node is the entity of the passed-in parameter, including the node value and the next address. *node = *next is a shallow copy. If the ListNode is complex, the shallow copy may cause some problems. Deep copy is recommended. ListNode is here:

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

This way is better to understand:

class Solution {
public:
    void deleteNode(ListNode* node) {
        ListNode* next = node->next;
        node->val = next->val;
        node->next = next->next;
        delete next;
    }
};
wuleice
  • 16
  • 3