How to add element at first and last position of linked list in Java?

LinkedList class in java.util package provides the addFirst() method to add an element at the start of the linked list (also known as head)  and addLast() method to add an element at the end of the linked list, also known as the tail of the linked list. Java's LinkedList class is an implementation of doubly linked list data structure but it also implements java.util.Deque interface and these two methods came from that interface, which means they are only available from Java 1.6 onward. addFirst() method insert the specified element at the beginning of the linked list and addLast() method insert the specified element at the end of the linked list.

LinkedList class is good if your program requires you to frequently add and remove element than searching for element because it provides O(1) performance for adding and removing element at start and end of the linked list but O(n) for searching an element in the list, because you need to traverse the whole list to find the specified element.

If you are serious about learning Java collection framework in detail then I suggest you take a loot at Java Generics and Collection book by Maurice Naftalin. It's one of the best books to master Java collection framework and quite useful from both learning and interview point of view.





Java Program to add element at start and end of the linked list 

Here is my Java solution to add an element at the head and tail position of doubly linked list in Java. Why doubly linked list? Isn't we are using LinkedList class from java.util package? Yes, that an implementation of doubly linked list. This is not the pure logic solution, instead, we are using the API method to solve the problem. Java's LinkedList class provides addFirst() and addLast() method to add an element at the start and end of the linked list. You can use them when your program requires adding messages at those positions.

You can also see Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about these useful collection classes in Java.

adding element at first and last position of linked list in Java


Now, here is our Java program to show how to add elements at the start and end of a linked list using addFirst() and addLast() method, remember it's a doubly linked list.

import java.util.LinkedList;

/**
 * Java Program to add elements at start and end of linked list
 * in Java. You can use addFirst() and addLast() method to
 * add an element at first and last position of linked list
 * in Java. 
 * 
 * @author java67
 */

public class StringRotateDemo {

    public static void main(String args[]) {

        // Creating a linked list of numbers in String format
        LinkedList<String> listOfNumbers = new LinkedList<>();
        listOfNumbers.add("100");
        listOfNumbers.add("200");
        listOfNumbers.add("300");
        listOfNumbers.add("400");
        listOfNumbers.add("500");
        
        // let's print the linked list before adding new number
        System.out.println("Original linked list : ");
        System.out.println(listOfNumbers);
        
        
        // let's add an element at start of the linked list
        listOfNumbers.addFirst("000");
        
        // now let's print the linked list again, 000 should
        // be at first position
        System.out.println("linked list after adding an element at start : ");
        System.out.println(listOfNumbers);
        
        
        // now let's add element at the end of the linked list
        // in Java
        listOfNumbers.addLast("600");
        
        // let's print the linked list again, 600 should be
        // the last element
        System.out.println("linked list after adding an element at end : ");
        System.out.println(listOfNumbers);
    }

}

Output
Original linked list : 
[100, 200, 300, 400, 500]
linked list after adding an element at start : 
[000, 100, 200, 300, 400, 500]
linked list after adding an element at end : 
[000, 100, 200, 300, 400, 500, 600]

That's all about how to add an element at first and last position of linked list in Java. You can use addFirst() and addLast() method to add an element at start and end of the list. Remember, the start of linked list is also known as head and end of the linked list is known as the tail, so this solution also applicable when you need to add an element at head and tail of a linked list in Java.

How to add element at first and last position of linked list in Java


If you like this tutorial and interested in learning more about different classes of Java Collection framework, you can also check out following articles :
  • How to remove elements from ArrayList in Java? (example)
  • How to get a first and last element from ArrayList in Java? (example)
  • How to synchronized ArrayList in Java? (example)
  • How to reverse order of elements in ArrayList? (example)
  • How to remove duplicate elements from ArrayList in Java? (example)
  • How to create and initialize ArrayList in the same line? (example)
  • How to loop over ArrayList in Java? (example)
  • How to get sublist from ArrayList in Java? (example)
  • How to convert ArrayList to String in Java? (example)
  • How to make read only ArrayList in Java? (example)


No comments:

Post a Comment