So I'm implementing a linked list in Java, and so far it looks like this:
public class List <T> {
private class Node {
T value;
Node next;
}
private Node node_at(int n) {
Node seeker = front;
for (int i = 0; i < n; i++) {
seeker = seeker.next;
}
return seeker;
}
Node front;
int length;
public int length() {
return length;
}
public void insert(int index, T o) {
if (index == 0) {
Node n = new Node();
n.value = o;
n.next = node_at(0);
front = n;
} else {
Node n1 = node_at(index-1);
Node n2 = new Node();
n2.next = n1.next;
n1.next = n2;
n2.value = o;
}
length++;
}
public void append(T o) {
Node n = node_at(length);
n.value = o;
n.next = new Node();
length++;
}
public void append(List<T> l) {
node_at(length-1).next = l.front; // specific area of interest
length += l.length;
}
public T at(int n) {
Node seeker = node_at(n);
return seeker.value;
}
List() {
length = 0;
front = new Node();
}
}
The line that I marked specific area of interest
is effectively cutting off a node (the last node in the receiver), leaving it homeless. Is that node going to get garbage collected?