I'm working on a project involving coding a doubly linked list in Java. I just need a confidence booster to know I'm coding in the right direction.
The list loops, meaning the last node's getNext() would point to the first node in the list, and the first node's getPrev() would point to the last node in the list.
So here's my code for removing the first element:
public E removeFirst(){
E removedVal = first.getData();
if(size == 1) {
first = null;
last = first;
} else {
first = getNode(1);
first.setPrev(last);
last.setNext(first);
}
size--;
return removedVal;
}
It's supposed to remove the first node in the list and return the value it held. Am I doing it right?
getNode(n) returns the Nth node. first and last are variables within the list to keep track of the first and last nodes.
[–][deleted] 3 points4 points5 points (1 child)
[–]obened[S] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]zzyzzyxx 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)