-1

When adding a node to the end of a linked list, the process below is used to find the last node and append a new node after it:

The new node is always added after the last node of the given Linked List. For example if the given Linked List is 5->10->15->20->25 and we add an item 30 at the end, then the Linked List becomes 5->10->15->20->25->30. Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node. (https://www.geeksforgeeks.org/linked-list-set-2-inserting-a-node/)

However, I am a bit confused as to how pointers actually work in this context. For example, when I do something like this:

int test = 4;
int pointer = test;
pointer += 1;

The value of test does not change, only the value of the pointer does. However, when adding a node to a linked list, a temporary pointer is used as follows:

Node last = head;  
while (last.next != null) {
    last = last.next; 
}    
last.next = new_node;

So essentially, at the very end, the last pointer is set equal to the last element of the linked list. Its next node is set equal to the new node being created. What I am confused about is how this impacts the original linked list. Wouldn't this simply set the next node of the pointer last rather than the actual next node of the last node of the linked list (in a similar manner as demonstrated with my int test above)?

mlz7
  • 2,067
  • 3
  • 27
  • 51
  • 1
    Please provide a [MCVE] (with emphasis on **complete** and **minimal**). Two remarks: References != Pointers. --- Never omit `{` and `}`, even if possible, this is a very common and hard to find source of bugs – Turing85 Mar 02 '19 at 22:27
  • @Turing85 my apologies – mlz7 Mar 02 '19 at 22:29
  • Possible duplicate of [How do linked list work?](https://stackoverflow.com/questions/6957976/how-do-linked-list-work) – Progman Mar 02 '19 at 22:55
  • @Progman my question is not specific to linked lists in any way, rather I am trying to understand how references work on a conceptual level – mlz7 Mar 02 '19 at 22:56
  • @Markoe7 Then check https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Progman Mar 02 '19 at 22:58
  • @Progman I did, maybe in the future try being helpful first and then voting to close a question after. – mlz7 Mar 02 '19 at 23:01
  • By suggesting a duplicate, Progman is trying to be helpful. – Mark Rotteveel Mar 04 '19 at 09:39

1 Answers1

2

Java does not have pointers.

It has "primitives" such as int which store values and "references" such as Node in your example that store a reference to an object.

So, in your first example you have two primitive variables, each stores a value and they are not connected in any way.
Assigning one to the other simply copies the value.

In your second example, next is a member of class Node.
last holds a reference to an object of class Node.
So, when you change the value of last it self, it simply points to another object.
But, when you change the value of a member of an object last points to, like this: last.next = new_node you change the inside of the object last points to.

This is why the change is preserved.

Lev M.
  • 6,088
  • 1
  • 10
  • 23