Atulya Arya
Atulya Arya

Reputation: 21

Difference in using * in Linked List in C++

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;
    }
};

Upvotes: 0

Views: 501

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

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.

Upvotes: 2

Zan Lynx
Zan Lynx

Reputation: 54325

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.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409156

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.

Upvotes: 1

Related Questions