Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Delete the last node in a linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Delete the last node in a linked list" Watch "Delete the last node in a linked list" New topic

Delete the last node in a linked list

Tim Chase

Joined: Oct 28, 2012
Posts: 1

I am having trouble walking through a linkedlist starting at the first node and moving to the last node while keeping track of thecurrent node and the previous node. Then once I find the last node I want to set it to null but I only want to delete the last node when a certain amount of elements have bee pushed onto the dropstack which is decided by the user. This is what I have so far.

I can not use an iterator to move through the linkedlist and I am trying to walk through the linked list in the push method so that when the count of the number of elements in the linkedlist stack is for example at 3 and the size of the linkedlist stack is 3 that the last node is deleted and the new element is pushed onto the top of the linkedlist stack. I have been stuck on this for a couple days now so any help would be awesome. Thanks.
Max Frank

Joined: Oct 29, 2012
Posts: 2
In your push(),You should judge whether count is equal to the size at first
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46352
Welcome to the Ranch, both of you.
TC: draw a diagram of your List on paper. Put arrows on each of the nodes showing how they connect to one another. Then see how you can iterate the List. If it is a stack, then it is usually a singly‑linked list, which you can only iterate one direction. So finding the bottom node means iterating the whole stack. It is finding it that is hard, not deleting it.
Get that working, without any count or anything. You now have a deleteLastNode() method.
Now add a deleteLastNodeIfSizeOver(int size) method, which can simply use an if statement to decide whether to call that other method. Give those methods either boolean return type, or return the E you have just deleted. Beware: if the size is small, those methods might return null.

By the way: it is not normal behaviour for a stack to delete its bottom element. You usually only access a stack from its top.
Why have you created that Exception class when there is one already in existence?
Red Smith
Ranch Hand

Joined: Aug 05, 2007
Posts: 136

You are right there. If the stack is A->B->C->D-> you need to get down to D to find out that it is the last node and null the next pointer in C. However, at that point you have lost any reference to C since there is no "prev" reference in D. So you need to save a previous node. You have a previous node, you set it, and you null the "next" pointer in previous. The problem is that you save to previous after the loop. But after the loop all you have is D to save.

If the value passed to the DropStack constructor is the maximum number of elements, than your logic should change as you are not treating it like it is the maximum amount of values to store.

There is another issue related to count in DropStack.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and change your DropStack#push name to DropStack#enqueue - it avoid confusion.
I agree. Here's the link:
subject: Delete the last node in a linked list
It's not a secret anymore!