recursion - Why is this linked list returning null? Java -


It is my code that the integer is placed in a linked list until user enters 1. It's fine, but in the end it's nothing printing. I do not know why the integer in the list is not being properly stored.

  read public static node (scanner scan) {int i = scan.nextInt (); Node n = new node (); If (i == -1) {return n; } And {n.data = i; N = n.next; Read (scan); Return n; }} Private static zero printlist (node ​​head) {if (head == empty) {} else {System.out.print (head.data + ""); Printlist (head.next); ...}} public static zero main (string [] args) {scanner scan = new scanner (System.in); Node head = read (scan); Printlist (head); }   

According to the request I added the main and print methods. I have also noticed that I am creating a new node in my recursive reading method.

How do I declare nodes in the way of reading while not creating additional nodes?

n = n.next

Is n.next at this point? Why are you setting this to your new node? My guess is that your problem is here, and that you are actually trying to set the new node n to the "next" value of the previous node. Is this sound right?

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -