How to add element at the last position of the LinkedList
Previous | Home | Next |
This example shows how to add the element at the Last position of the LinkedList
To add the element at the last position of the LinkedList use addLast () method .
public void addLast(E e)
- Appends the specified element to the end of this list.
Call the addLast () method through LinkedList object along with the parameter
package Example;
import java .util.LinkedList;
import java .util.Iterator;
public class LinklistExample {
/**
* @param args
*/
public static void main(String[] args) {
LinkedList Lst= new LinkedList(); // create object of LinkedList
// add elements to the LinkedList using add method
Lst.add("4");
Lst.add("6");
Lst.add("8");
Lst.add("10");
Lst.add("12");
Lst.add("14");
Lst.add("16");
Lst.add("18");
System.out.println("The elements of LinkedList prior to addition of other elemnts are:"+Lst);
Lst.addLast("20"); // add element at the last of linklist
System.out.println("Now the LinkedList contains:"+Lst);
The elements of LinkedList prior to addition of other elemnts are:[2,4, 6, 8, 10, 12, 14, 16, 18]
after adding element Now the LinkedList contains:[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Previous | Home | Next |