Question
//Some linkedlist practice but failing to run, could you please tell me and fix where it is going wrong //Best regards class Node { int
//Some linkedlist practice but failing to run, could you please tell me and fix where it is going wrong
//Best regards
class Node { int number; Node next; //we are making an variable of Type Node called next;
class Linked { Node root; public Linked() { root=null; } public Node getNewNode(int number) { Node a = new Node(); //this new node a.next= null; a.number= number; return a; } public Node insert(int number, Node node) { //since this is the beginning of our linked list we need to check if it is empty //let's check if node is null if (node==null) { return getNewNode(number); } else { node.next= insert(number, node.next); } return node; } @Override public String toString() //remember we want all the elements of list print but null {//making method to print our linkedlist if(next != null) { return number + "\t" + next; } else { return number+ "\t"; } } } public class LinkedListYT {
public static void main(String[] args) { // TODO Auto-generated method stub Node root= null; Linked list= new Linked(); root= list.insert(12, root); //this is passing to function insert System.out.println(list); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started