Draw the object diagram to figure out how this program will behave.

public class TestNode {
    public static void main(String [] args) {
	Node n1 = new Node(1);
	Node n2 = new Node(2);
	Node n3 = new Node(3);

	n1.setNext(n2);
	n3.setNext(n2);
	n2.setNext(n3);

	int data = n1.getNext().getNext().getData();
	System.out.println(data);

	n2 = n3;

	data = n2.getData();
	System.out.println(data);

	n3.setNext(null);
	
	data = n2.getNext().getData();
	System.out.println(data);
    }
}

This is an example from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.