Add elements at beginning and end of LinkedList in Java

In this tutorial, we learn how to add elements at beginning and end of LinkedList in Java.

LinkedList in Java is doubly-linked list implementation of the List and Deque interfaces. LinkedList class extends AbstractSequentialList and implements the List, Deque, and Queue interfaces and permits null elements. It provides a linked-list data structure.

LinkedList class in Java provides addFirst() and addLast() methods to add elements at beginning and end of LinkedList. The syntax of addFirst() and addLast() method is:

Syntax of addFirst() method:

this method inserts the specified element at the beginning of this list.

Syntax of addLast() method:

this method appends the specified element to the end of this list. This method is equivalent to add(E).

Example:

Output:

 

Related posts

Leave a Comment