Pass objects to helper methods in JAVA

I am trying to write a program that reverses a given Linked List . Firstly , I will read the number from the user and pass it to a function that converts the number to a LinkedList .Then I will pass the linked list to a helper function which will reverse the given LinkedList .

This is my program :

import java.io.*;
import java.util.*;


public class Solution {

    public class Link {
        Node head;

        public class Node {
            int data;
            Node next;
            Node(int x) {
                data = x;
                next = null;
            } 
        } // End of class Node

        public void printList() { 
            Node n = head; 
            while (n != null) { 
                System.out.print(n.data+" "); 
                n = n.next; 
            } 
        } 

        public void createList(String num) {
            this.head = new Node(num.charAt(0));
            Node prev = head ;
            for(int i = 1; i < num.length(); i++) {
                Node newNode = new Node(num.charAt(i));
                prev.next =  newNode;
                prev =  newNode;
            }
        }
    } // end of class Link

    public Node reverse(Node curr) {
        if (curr == null) return null;
        if(curr.next != null) {
            Node forward = reverse(curr->next);
            curr.next.next = curr ;
            curr.next = null;   
        } else {
            return res;
        }
        return forward;
    }    

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String number1 = sc.next();
        // System.out.println(number1);

        String number2 = sc.next();
        //System.out.println(number2);

        Link first = new Link();
        first.createList(number1);
        first.printList();

        System.out.println("''''''''''");
        Link second = new Link();
        second.createList(number1);
        second.printList();
        System.out.println("''''''''''");

        first.head = reverse(head); 
        first.printList();
        System.out.println("''''''''''");

        second.head = reverse(head);
        second.printList();
        System.out.println("''''''''''");
    }
}

This is the error I am getting :

Solution.java:47: error: cannot find symbol
  public Node reverse(Node curr)
                      ^
  symbol:   class Node
  location: class Solution
Solution.java:47: error: cannot find symbol
  public Node reverse(Node curr)
         ^
  symbol:   class Node
  location: class Solution
2 errors

My question is even though both the class Link and the class Node are public why the method reverse is not able to access the symbol Node? And how the helper methods in Java should be written so that they access the objects of any classes.

1 answer

  • answered 2019-03-19 15:25 Koray Tugay

    The problem is your classes are inner classes and an instance of inner class can only live with an instance of an outer class. Here is a possible solution if you make the classes top level, things should be fine (you have to fix the reverse method as well)

    class Node {
        int data;
        Node next;
    
        Node(int x) {
            data = x;
            next = null;
        }
    }
    
    class Link {
    
        Node head;
    
        public void printList() {
            Node n = head;
            while (n != null) {
                System.out.print(n.data + " ");
                n = n.next;
            }
        }
    
        public void createList(String num) {
    
            this.head = new Node(num.charAt(0));
            Node prev = head;
            for (int i = 1; i < num.length(); i++) {
                Node newNode = new Node(num.charAt(i));
                prev.next = newNode;
                prev = newNode;
            }
        }
    }
    
    public class Solution {
    
        public Node reverse(Node curr) {
            // to be implemented
            return null;
        }
    }