Thursday, September 28, 2023

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

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 a 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 elements than searching for elements because it provides O(1) performance for adding and removing elements 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 the Java collection framework in detail then I suggest you take a look 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 points of view.





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

Here is my Java solution to add an element at the head and tail position of a doubly linked list in Java. Why doubly linked list? Isn't we are using the LinkedList class from java.util package? Yes, that implementation of a 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() methods 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.

How to add 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 the 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 the first and last position of the linked list in Java. You can use addFirst() and addLast() methods to add an element at the start and end of the list. 

Remember, the start of the linked list is also known as the 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 the head and tail of a linked list in Java.

If you like this tutorial and interested in learning more about different classes of the Java Collection framework, you can also check out the 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)
Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any issues or feedback then please drop a comment. 

And lastly one question for you? What is the difference between ArrayList and LinkedList in Java? 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.