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.