Question: import java.util.Scanner; class IntNode { int data; IntNode next; IntNode ( int data ) { this.data = data; this.next = null; } void insertAfter (
import java.util.Scanner;
class IntNode
int data;
IntNode next;
IntNodeint data
this.data data;
this.next null;
void insertAfterIntNode newNode
newNode.next this.next;
this.next newNode;
public class LabProgram
Recursive method to print linked list
public static void printLinkedListIntNode head
Base case: if head is null, return
if head null
System.out.println;
return;
else
Print current node's data
System.out.printheaddata;
If next node is not null, print comma
if headnext null
System.out.print;
Call printLinkedList recursively with next node
printLinkedListheadnext;
public static void mainString args
Scanner scnr new ScannerSystemin;
int size scnrnextInt;
int value scnrnextInt;
IntNode headNode new IntNodevalue; Make head node as the first node
IntNode lastNode headNode; Node to add after
IntNode newNode null; Node to create
Insert the second and the rest of the nodes
for int n ; n size ; n
value scnrnextInt;
newNode new IntNodevalue;
lastNode.insertAfternewNode;
lastNode newNode;
Call printLinkedList with the head node
printLinkedListheadNode;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
