In some programming scenarios, we often need to record a particular instance location (such as the head node of the linked list that needs to be recorded in the BST-to-bidirectional link list).
In the Java programming process, you need to be aware of the issues that are referenced.
Class ListNode {public int val;public listnode next;public listnode (int _val) {val = _val;}} public class Pointertestinjava {public static void main (string[] args) {ListNode Node1 = new ListNode (1); ListNode Node2 = Null;pointertest (Node1, Node2), if (Node2 = = null) {System.out.println ("Node2 is still null!");} else {Sys Tem.out.println (Node2.val);}} public static void Pointertest (ListNode node1, ListNode node2) {node2 = Node1;}}
The printing results are as follows:
NULL!
Obviously the Pointertest method does not point the Node1 to Node2, making the following changes:
public class Pointertestinjava {public static void main (string[] args) {ListNode Node1 = new ListNode (1); ListNode node2 = new ListNode (2);p ointertest (Node1, Node2), if (Node2.next = = null) {System.out.println ("Node2.left is nul L! ");} else {System.out.println ("Node2.next is" + Node2.next.val);}} public static void Pointertest (ListNode node1, ListNode node2) {node2.next = Node1;}}
Printing results:
Node2.next is 1
At this point, node1.left points to the node2. The reason is that node1.left can be seen as a pointer.
References and pointers in Java