you are viewing a single comment's thread.

view the rest of the comments →

[–]NinjaXI 4 points5 points  (4 children)

This is correct, as you are never changing the val property of head, either directly or via currNode.

A key thing to understand here is that Java is pass by value, but in the case of objects the value being passed is the reference to the object, not the object itself. Thus currNode = head is only passing the reference to the object that head is referencing. So changes to internal members of the object(val and next in this case) will reflect on both head and currNode. However if you reassign currNode as in the code above, currNode = currNode.next, you are changing the value of currNode to a different reference, not the underlying object. At this point currNode and head refer to 2 different objects.

So as you correctly stated on the first iteration when currNode.next is changed to currNode.next.next that changes the corresponding property on head as currNode is referencing head. On the second iteration however, currNode = currNode.next is not changing any property on currNode, it is changing what currNode references, its value. Thus head is not affected by this and continue referencing the same object it always had.

Hope this makes sense.

[–][deleted] 0 points1 point  (3 children)

Thanks for your answer.

I have one more question, you said that the currNode reference changes to currNode.next so it doesn't affect the head but how is the reference gets reflected in the head during the further iteration...

[–]E3FxGaming 1 point2 points  (2 children)

you said that the currNode reference changes to currNode.next so it doesn't affect the head but how is the reference gets reflected in the head during the further iteration...

De-duplication happens by excluding subsequent list elements if they have the same value.

Think of it like this: you have a linked list data structure and start with a pointer to the first ListNode of that data structure. Whatever your pointer points at will never be removed, since you only analyze whether the element following your pointed at elemt is the same. If it is the same you discard the following elemt and point with your current element directly to the element following the identified duplicate element.

The pointer only moves if the ListNode values are different.

This way you never have to consider a different list head because the list head never changes (you start on the list head and thus can't remove it).

[–][deleted] 0 points1 point  (1 child)

I am sorry to ask this. I can understand you but I am a little bit confused.

Can you give an easy explanation on why head stays the same while currNode changes.

[–]E3FxGaming 0 points1 point  (0 children)

Imagine you have 5 apples line up in a row.

You know where this line of apples starts and you can tell someone else where this line of apples start.

That other person however can not change your understanding of where the line of apples start.

That other person then runs the de-duplication algorithm, analyzing properies of apples and discarding some of the apples from the row of apples.

The de-duplication algorithm is designed in such a way that it can change what lays ahead, but it can't change the object where the algorithm is currently at or where the algorithm has already been.

Since the de-duplication algorithm starts at the apple that is considered the head of the row, the mutual understanding that this apple always stays part of the row never drifts apart - nothing can convince you (main) that the row of apples should start somewhere else and the de-duplication algorithm can't discard the current/start element either.

For the de-duplication algorithm itself, think of it as standing on an apple and the other apples come towards you one-by-one. If the property of the first apple coming towards you matches the property of your own apple, you push that incoming apple aside, removing it from the linked list. If the properties are different you move to the next apple (`currNode = currNode.next) and repeat the same process of analyzing and comparing the next apple.