There’s no good reason for size2 to be coded the way that it is. What is the source of that code, for I think that it may have a different purpose other than being useful.
i am just playing around with that code.Just i want to why it is behaving like that because in size1 we are storing only a reference to variable node.so this variable is pointing to same object in the size2 method but why it is not changing the head value?
With size1, you have a separate variable called node that has a reference to the head object. It’s as if the node variable is pointing at the head object, without actually being it. So you can change what the node points at without changing the actual head object.
Variables assigned to objects or arrays are assigned by reference. Other things like strings, numbers, booleans are assigned by value instead. When assigning by value, the value is copied to the new variable. When assigned by reference, the variable points to the referenced item instead.
There you are changing something in the object. With the node1 code, you are not changing anything in the head object. Instead you are assigning the node variable to point to the next object.