1

So I'm trying to solve one of the LeetCode questions using a LinkList however when I try to add a node (Node q = new Node();) it wasn't working. So I made a Node class and put it in a package. It still gives me an error (Node q = new Node(); symbol: class Node) when I'm trying to compile it.
Here is my Node class:

package newpackage; 
public static class Node<E> {
    E item;
    Node<E> next;
    Node<E> prev;

    Node(Node<E> prev, E element, Node<E> next) {
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}

and here is the problem that I'm trying to solve:

import stdlib.*;
import dsa.*;
import java.util.*;
import newpackages.*;
public class TwoNum {
public static LinkedList addTwoNumbers(LinkedList<Integer> l1, LinkedList<Integer> l2) {
    Node q = new Node();
    q = l1;
    node p = new node();
    p = l2;
    LinkedList<Integer> end = new LinkedList<>();
    while (q != null && p != null) {
        int sum = 0;
        int caryy = 0;
        int x = p.element();
        int y = q.element();
        if (p == null) {
            x = 0;
        }
        if (q == null) {
            y = 0;
        }
        sum = x + y + caryy;
        caryy = sum / 10;
        end.addFirst(sum % 10);
        p = p.next();
        q = q.next();
    }
    return end; 
}
public static void main(String[] args) {
    LinkedList<Integer> a = new LinkedList<Integer>();
    LinkedList<Integer> b = new LinkedList<Integer>();
    LinkedList<Integer> c = new LinkedList<>();
    a.add(2);
    a.add(4);
    a.add(3);
    b.add(5);
    b.add(6);
    b.add(4);
    c = addTwoNumbers(a,b);
    node m = c;
    while (m != null){
        StdOut.print(m.element());
        m = m.next();
    }
    StdOut.println();
}

I would really appreciate it if anyone can help me with this.

greybeard
  • 2,102
  • 7
  • 24
  • 58
  • 1
    A top level class cannot be static. – trincot Jun 01 '21 at 20:04
  • 1
    Does this answer your question? [Java inner class and static nested class](https://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) – Yoshikage Kira Jun 01 '21 at 20:05
  • (Do not import "everything". If you are using an IDE, consult it's documentation; the may be something like *organise imports*.)(I see `Node p` as well as `node q`.) – greybeard Jun 02 '21 at 04:47
  • Program against *interface*s (`java.util.List<>`) instead of concrete *types* (`java.util.LinkedList`). This will avoid any temptation to tinker with internals (or use funny methods like `element()`). (Revisit the problem statement: lists and "integers". No mention of *node* whatsoever.) – greybeard Jun 02 '21 at 04:59
  • `Node` class is in package `newpackage`, but TwoNum imports `newpackages.*`, so it's not being imported at all. There's also a `Node` class and a `node` class being referenced. And `Node` is a static class for some reason...? – Roddy of the Frozen Peas Jun 02 '21 at 06:37

0 Answers0