import javax.swing.*;
class Node
{
int no;
Node ptr;
//constructor of class Node
Node(int n, Node p)
{
no = n;
ptr = p;
}
//display list
static void printList(Node list){
Node node =list;
String out=””;
while (node !=null)
{
out += node + ” –> [” + node.no + “] ” + node.ptr + “\n” ;
node =node.ptr;
}
JOptionPane.showMessageDialog(null,”link list \n”+out);
}
//main method
public static void main(String args[])
{
int noNodes = Integer.parseInt(JOptionPane.showInputDialog(“number of nodes: “));
if (noNodes>=2 && noNodes<=5)
{
int ctr = 1;
Node head, dummy, node;
int value = Integer.parseInt(JOptionPane.showInputDialog(“number ” + ctr));
//1st node created
node = new Node(value, null);
head = node;
ctr++;
//2nd upto last node created
while(ctr<=noNodes)
{
value = Integer.parseInt(JOptionPane.showInputDialog(“number ” + ctr));
dummy = new Node(value,null);
node.ptr = dummy;
node = dummy;
ctr++;
}
//call static method printList
printList(head);
}
}}
output