In Java (or any OOP language), I can make a nested class "Node", that represents some object that contains int data. I can then declare a "Node" reference with variable named "head":
public class ListNode {
class Node {
int value;
Node next = null;
public Node(int value) {
this.value=value;
}
}
Node head;
}
I can then do something like:
head = new Node(42);
...which should make the "head" reference point to a new Node object with int value 42, and a reference inside of it pointing to null:
But how can I actually make another reference that points to the "head" reference? I don't think its possible, but it would look like this:
I know that if I make another reference, like Node another_ref;
and then do another_ref = head
, all that will happen is another_ref
will point to whatever head was pointing to (Node that contains 42 in this case), so I will be left with 2 references pointing at a Node object with int value = 42, and Node next reference that points to null.
I know that I can take head.next
's reference, which is the Node object's reference pointing to null, and I can point it back at itself by doing head.next = head;
, but it doesn't seem like I can get anything to point back to the Node head
reference.
Am I missing something here? Can you not "chain" or "link" together references like this? Obviously I can create a bunch of new Nodes(integer values...)
with various integer values to chain-together a linked list, but just curious about this.