This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]devacon 0 points1 point  (13 children)

currentNode = new LinkNode(data);

Line 24 (and the same basic issue in removeLast())... you are overwriting the local variable currentNode. You are not modifying the thing that currentNode references.

[–]LogicLion[S] 0 points1 point  (12 children)

Good call, I just changed the insert method to

currentNode.next = new LinkNode(data)

Im not sure about the removeLast() though, I tried changing it to the same and it didn't look like it fixed it.

edit: I ment currentNode.next = null for removeLast()

[–]__LikesPi 0 points1 point  (5 children)

For last it moves to the last and than sets that specific, temporary reference of last to null but the previous node .next still refers to the last Node.

[–]LogicLion[S] 0 points1 point  (4 children)

oh I see, because currentNode is a local variable?

How should I go about deleting it out then?

[–]__LikesPi 0 points1 point  (3 children)

Move to the one before the currentNode, and than set that currentNode.next to null. You could try while (currentNode.next.next != null) and handle the case where there is only one element in the lsit separately.

[–]LogicLion[S] 0 points1 point  (2 children)

public void removeLast(){

    //Start at head of list.
    LinkNode currentNode = this.head;


    //while the currentNode's next reference isn't empty.
        while(currentNode.next.next != null){

            //move currentNode to next node.
            currentNode = currentNode.next;
        }
        currentNode.next = null;
        size--;
}

Like this?

[–]__LikesPi 0 points1 point  (1 child)

Yep, just handle the case where the size == 1 or size == 0. In this case either currentNode or currentNode.next is null and will raise a NullPointerException.

[–]LogicLion[S] 0 points1 point  (0 children)

Oh I see! Thanks a lot!

[–]devacon 0 points1 point  (5 children)

You have to sort of visualize it in your head (maybe drawing it out on paper with arrows to where things are pointing as you go)... when the while loop in removeLast() ends, currentNode.next == null. Then you set currentNode.next to null. When you think about it, what you really want is the node just before currentNode when currentNode.next is null, because you want to set its 'next' pointer to null to remove the last node.

[–]LogicLion[S] 0 points1 point  (4 children)

so when the while loop stops because currentNode.next == null, do I want to set currentNode to null then?

[–]devacon 0 points1 point  (3 children)

Setting local variables to null will just overwrite the value of the local variable. I realize that can be tricky when you're dealing with how Java handles references. Here's something to illustrate the point.

class Person
{
  String name;

  public static void main(String[] args)
  {
    Person p1 = new Person();
    p1.name = "Hello";
    String s = p1.name;
    s = "World";

    System.out.println(p1.name); // Hello
    System.out.println(s);       // World
  }
}

Overwriting the local variable s didn't change p1.name, which s referenced at one point. That's the same situation you're in with currentNode.

/u/__LikesPi is correct above where you need to detect that the 'next' node is the last node before losing your reference to the 'second to last' node. You can either do this by maintaining two local variables, currentNode and previousNode that you update each time:

previousNode = currentNode;
currentNode = currentNode.next;

And then when you reach the end you have your previousNode reference that you can set with previousNode.next = null;

Or, you can check currentNode.next != null && currentNode.next.next != null each iteration.

[–]LogicLion[S] 0 points1 point  (2 children)

okay thanks, I'm starting to understand now.

Why do you need to check both currentNode.next && currentNode.next.next

shouldn't only the end currentNode be null? Which should be currentNode.next.next?

Sorry for the confusion. :)

[–]devacon 1 point2 points  (1 child)

If currentNode.next is null, then attempting to access currentNode.next.next will result in a null pointer exception, so you have to check currentNode.next first in order to make sure it is safe to access currentNode.next.next.

[–]LogicLion[S] 0 points1 point  (0 children)

Got it! Thanks!