Hi it's been a while since I've written java and I can't seem to find what is wrong with this code. I'm implenting deleting a node from a linked list but my program won't compile. I keep getting:
error: non-static variable this cannot be referenced from a static context
Node head = new Node();
It has an error for all my new Node() instances in my main method.
public class NodeDelete{
class Node {
int data;
Node next;
public Node(){ }
}
Node Delete(Node head, int position) {
// Complete this method
int index = 0;
Node current = head;
if (position == 0 ){
head = head.next;
}
else{
while (index < (position - 1)){
current = current.next;
}
current.next = current.next.next;
}
return head;
}
public static void main(String[] args) {
Node head = new Node();
head.data = 0;
Node node1 = new Node();
node1.data = 1;
Node node2 = new Node();
node2.data = 2;
head.next = node1;
node1.next = node2;
}
}