answersLogoWhite

0

Well if you're using the build in LinkedList class, you can simply call list.removeLast().

But let's assume you're not:

class LinkedList {

LinkedListNode root;

public void removeLast() {

// special case for an empty list

if( root null ) {

root = null;

return;

}

// iterate through list, starting at root

LinkedListNode last = null;

LinkedListNode current = root;

// when current.next is null, current is the last element of this list and

// last will become the new last element

while( current.next != null ) {

last = current;

current = current.next;

}

// cut off current

last.next = null;

}

private class LinkedListNode {

Object data;

LinkedListNode next = null;

}

}

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


Can you provide a solution to the diamond-square algorithm using Java and recursion?

Yes. It is possible to provide a solution to the diamond-square algorithm using Java and recursion.


What is the difference between an algorithm and java code?

In Java programming language, an algorithm refers to a sequence of instructions that have been specified to undertake a particular task within a certain time. An algorithm can take no or several inputs but will generate at least one output.


Java source code for median filter algorithm?

research is going on to develop a source code for median filtering using java


Sorting an array of numbers in java?

here you will a good example on java sorting algorithm application http://javacodespot.blogspot.com/2010/08/java-sorting-animations.html http://javacodespot.blogspot.com/


How can the quicksort algorithm be implemented with a 3-way partition in Java?

To implement the quicksort algorithm with a 3-way partition in Java, you can modify the partitioning step to divide the array into three parts instead of two. This involves selecting a pivot element and rearranging the elements so that all elements less than the pivot are on the left, all elements equal to the pivot are in the middle, and all elements greater than the pivot are on the right. This approach can help improve the efficiency of the quicksort algorithm for arrays with many duplicate elements.


How do you write algorithms of java programs?

Write a program that graphically demonstrates the shortest path algorithm


What do you do when it says you have no space o download the Java for the limewire to work what do you delete then?

You need to uninstall unused programs or delete old and unneeded movies or documents. Java will need about 80 MB free, if I recall.


Which of the class in java.io package defines a method to delete a file?

The java.io.File class in Java has a delete() method to delete the file. See related link for details.


Do you need java 5 and 6?

No, you can delete the old one. Java does not remove old versions when it is updated, however I think they have fixed that now.


How do you get TOOMANYItems for Minecraft?

%appdata% open mincraft java archive Delete METAINF Download TMI Ctrl+a & drag into your minecraft java archive


What is java joptionpane?

Java's jOptionPane from the javax.swing library is a GUI element for, essentially, an option dialogue box.