This is my code for the linked list (not the main). The method "addLast" gives me the following error and i'm not sure how to resolve it: "non-static variable cannot be referenced from a static context". It is talking about this line: return new Node(x,null); I'd appreciate any help as to how to resolve this issue. Thank you
public class LinkedList
{
private class Node
{
int item;
Node link;
public Node ()
{
item = Integer.MIN_VALUE;
link = null;
}
public Node (int x, Node p)
{
item = x;
link = p;
}
} //End of node class
private Node head;
public LinkedList()
{
head = null;
}
//adds a node to the start of the list with the specified data
//added node will be the first node in the list
public void addToStart(int x)
{
head = new Node(x, head);
}
//adds a number at end of list
public static Node addLast(Node header, int x)
{
// save the reference to the header so we can return it.
Node ret = header;
// check base case, header is null.
if (header == null) {
return new Node(x, null);
}
// loop until we find the end of the list
while ((header.link != null)) {
header = header.link;
}
// set the new node to the Object x, next will be null.
header.link = new Node(x, null);
return ret;
}
//displays the list
public void printList()
{
Node position = head;
while(position != null)
{
System.out.print(position.item + " ");
position = position.link;
}
System.out.println();
}
}