0

What is the difference between (*current = *current->next)and (current = current->next)? My code is:

class Solution { public: ListNode* deleteDuplicates(ListNode* head) { ListNode* current = head; while(current!=NULL) { while (current->next!=NULL && current->val==current->next->val) *current = *current->next; current = current->next; } return head; } }; 
5
  • 4
    do you understand the difference between *p = *q and p = q where p and q are pointers? Commented May 15, 2020 at 17:28
  • current = current->next is the one you want. The other one is actually copying data around and overwriting stuff - not what you want. Commented May 15, 2020 at 17:32
  • The * operator before a pointer, dereferences the pointer, returns the value at the location pointed to by the pointer. Without the * operator, only the pointer contents are copied (not the thing pointed to by the pointer). Commented May 15, 2020 at 17:32
  • *current = *current->next; seems like it will cause a memory leak. Commented May 15, 2020 at 17:32
  • thanks a lot for clearing the concept! Commented May 20, 2020 at 10:35

3 Answers 3

2

In this statement

*current = *current->next; 

the object of the type ListNode pointed to by the pointer current->next is assigned to the object of the same type pointed to by the pointer current.

In this statement

current = current->next; 

the value of the pointer current->next is assigned to the pointer current.

As for the function itself then it should be defined at least the following way if its purpose is deleting adjacent nodes with duplicated values.

ListNode * deleteDuplicates( ListNode *head ) { for ( ListNode *current = head; current != NULL; current = current->next ) { while ( current->next != NULL && current->val == current->next->val ) { ListNode *tmp = current->next; current->next = current->next->next; delete tmp; } } return head; } 

Also it seems the function could be declared as a static member function like

static ListNode * deleteDuplicates( ListNode *head ) { //... } 

And instead of the null pointer constant NULL you should use the null pointer literal nullptr.

Sign up to request clarification or add additional context in comments.

Comments

1

The dereference operator * gets the value that the pointer is pointing to.

The statement

*current = *current->next; 

assigns from the object that current->next is pointing to, to the object that current is pointing to.

Now when you don't use the dereference operator:

current = current->next; 

you assign the pointers, not the objects they point to.

After this assignment then you have two pointers both pointing to the same object.


It might be easier to understand if we draw it out using pen and paper. Draw an object using a rectangle, and label it appropriately. Then draw another rectangle and connect it to the first using an arrow.

This second rectangle is pointing to the first rectangle, just like a pointer does in C++.

By using the dereference operator * you follow the arrow to the first rectangle.

So lets say we have the object node:

 +------+ | node | +------+ 

Then we create a pointer current that points to node:

 +---------+ +------+ | current | --> | node | +---------+ +------+ 

When you use current you get the pointer itself. When you use *current you follow the arrow and get node.

Comments

1

Quick lesson in pointers

This declares a pointer variable:

ListNode* current; 

This declares a ListNode variable:

ListNode node; 

This sets the pointer to point at node:

current = &node; 

This * operator dereferences current and the following two lines mean exactly the same thing:

std::cout << *current << '\n'; std::cout << node << '\n'; 

So when you write *current that is whatever current is pointing at, which is a ListNode.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.