I am trying to create a linked list taking user input and print the same, however, my linked list is getting created but I am unable to print
it. Here is the code below:
import java.util.Scanner;
class Node
{
int data;
Node next;
}
public class LinkedList
{
public static void create(Node start)
{
Scanner sc = new Scanner(System.in);
Node a = new Node();
System.out.println("Enter Details:");
a.data = sc.nextInt();
a.next = null;
start = a;
System.out.println("Do you want to continue(Y/N)?");
char ch = sc.next().charAt(0);
while(ch!='n')
{
Node b = new Node();
System.out.println("Enter Details:");
b.data=sc.nextInt();
b.next = null;
a.next = b;
a=b;
System.out.println("Do you want to continue(Y/N)?");
ch = sc.next().charAt(0);
}
sc.close();
}
public static void display(Node start)
{
Node temp = start;
while(temp!=null)
{
System.out.println(temp.data);
temp = temp.next;
}
}
public static void main(String[] args){
Node start=null;
create(start);
display(start);
}
}
I am unable to find the problem please help me find how to print
my linked list.