Java Help - Linked Lists

If you don’t know what Linked Lists are, you shouldn’t be here. I’m trying to figure out how to remove an object from the beginning of a linked list. Here’s the files I have so far; the //??? is what I’m stuck on.

HW11.java (Driver), LinkedList.java, Node.java

//if there is only one node, delete the entire list.
if (list.next == null) list = null;
//otherwise, make the first node point to the second node.
else list = list.next;

Let me know if that works.

Hey, it worked! Awesome!

… and I TOTALLY screwed up in the ListNode.toString() n.n;

On second thought, you can actually do it in one line:
list = list.next;

Look at the code again and you’ll see why. :sunglasses:

As long as you always set next to null beforehand (constructors work wonders for this), Cid’s code should work. I could totally show you how to do it in C++, it’s the same concept.

By default objects are set to null in Java, so you don’t have to explicitly do that.

That would be damn nice.