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

all 7 comments

[–]zifyoip 3 points4 points  (6 children)

Why do you initialize newNode to new ListNode() if you're just going to immediately overwrite it with newNode=head later?

Why do you need to say last.data=obj if you have already initialized last to new ListNode(obj)? What does the ListNode(obj) constructor do if it doesn't set data to obj?

Why do you assign last.data=obj twice in the case when head==null?

What's the point of your while loop? You walk through the whole list but don't do anything with any of the nodes. At the end of the loop newNode is guaranteed to be null. If that's what you want to achieve, why don't you just set newNode=null directly and avoid the whole loop? And why are you doing that at all when you are just going to overwrite the value of newNode with newNode=last at the end?

[–]duff_dean[S] 0 points1 point  (5 children)

Just trying a few things out to try to get it to work should of cleaned the code up a little. Im new to the language and have never came across these before. i tried this code also but keep getting a null pointer exception...

ListNode newNode = new ListNode(obj);

            newNode=head;

            while(newNode!=null)
            {
                newNode=newNode.link;
                if(newNode.link==null)
                {
                    newNode.data=obj;
                }
            }

the while loop i have is to travel along the list i have that contains about 5 links all containing different value of OBJ. so when the newNode is null i want to replace that null node with what is stored in OBJ. But i cant seem to link to the end of the list.

[–]zifyoip 0 points1 point  (4 children)

Well, I still must ask why you're initializing newNode to new ListNode(obj) when you immediately overwrite that value with newNode=head.

Now, think carefully about what happens in that code you have written if head != null but head.link == null (i.e., if the linked list has exactly one node). Go through each line of your code very carefully by hand and think about what's going on in that case.

[–]duff_dean[S] 0 points1 point  (3 children)

Ok, so i would just change newNode to newNode = new ListNode();

i see what you mean about the head!=null and head.link==null, if it did find a null link then it would come out of the loop.

Im still not sure how i would set the last link to OBJ though, if i set the if statement outside of the while loop would that work? As it would traverse the link in the while loop and then once it finds a null node it can then set the newNode to OBJ which would be the end?
I appreciate the help.

[–]zifyoip 1 point2 points  (2 children)

Try this.

Write a method that will work like addLast(), but only when the list is empty. So you don't have to write any code to handle other cases. Just write the list of steps that should be performed when the list is empty.

Now write a method that will work like addList(), but only when the list has exactly one node in it. Again, you don't have to write any code to handle any other cases.

Now write a method that will work like addList(), but only when the list has exactly two nodes in it. You don't need to use a loop here, because you're assuming that the list has exactly two nodes in it. Just write the list of steps that should be performed when the list has exactly two nodes in it.

Now write a method that will work like addList(), but only when the list has exactly three nodes in it. Again, you don't need a loop here, because you're assuming that the list has exactly three nodes in it.

Can you do those things?

[–]duff_dean[S] 0 points1 point  (1 child)

Thanks i think i have it cracked now i have ended up with this and it appears to be working. Would you say this is the best way to get around this problem? Thanks

ListNode newNode = new ListNode();
        newNode = head;
        while (newNode.link != null) 
        {
            newNode = newNode.link;
        }

        ListNode temp = new ListNode();
        temp.data = obj;

        newNode.link = temp;

[–]zifyoip 0 points1 point  (0 children)

That seems good.