A LinkedList in Java allows elements to be inserted at both the beginning and the end of the list. The addFirst() and addLast() methods provide a simple way to perform these operations.
- addFirst() inserts an element at the beginning of the LinkedList.
- addLast() inserts an element at the end of the LinkedList.
- The add() method can also be used to add an element at the end of the list.
Example:
Input: LinkedList = [10, 20, 30]
Insert at first = 5
Insert at last = 40Output: [5, 10, 20, 30, 40]
Methods to Add Elements at First and Last Position
There are several ways to add elements at the beginning and end of a LinkedList:
1. Using addFirst() and addLast()
The LinkedList class provides two methods specifically for adding elements at its ends.
addFirst():
The addFirst() method inserts the specified element at the beginning of the list.
Syntax of addFirst(): linkedList.addFirst(element);
addLast():
The addLast() method inserts the specified element at the end of the list.
Syntax of addLast(): linkedList.addLast(element);
Approach
- Create a LinkedList.
- Add some elements using add().
- Use addFirst() to insert an element at the beginning.
- Use addLast() to insert an element at the end.
- Print the updated list.
import java.util.LinkedList;
public class AddFirstLast{
public static void main(String[] args)
{
// Create a LinkedList
LinkedList<Integer> list = new LinkedList<>();
// Add elements to the list
list.add(10);
list.add(20);
list.add(30);
System.out.println("Original LinkedList: " + list);
// Add element at the first position
list.addFirst(5);
// Add element at the last position
list.addLast(40);
System.out.println("Updated LinkedList: " + list);
}
}
Output
Original LinkedList: [10, 20, 30] Updated LinkedList: [5, 10, 20, 30, 40]
Explanation
The list contains [10, 20, 30]. The addFirst(5) method places 5 before the first element, while addLast(40) places 40 after the last element.
2. Using add() Method to Add an Element at the Last
The add() method can also be used to insert an element at the end of a LinkedList. By default, when add(element) is called, the new element is appended to the end of the list.
Approach
- Create a LinkedList.
- Add elements using add().
- Use addFirst() to add an element at the beginning.
- Use add() to add an element at the end.
- Display the updated list.
import java.util.LinkedList;
public class AddFirstLast{
public static void main(String[] args)
{
LinkedList<String> list = new LinkedList<>();
list.add("B");
list.add("C");
list.add("D");
System.out.println("Original LinkedList: " + list);
// Add element at the beginning
list.addFirst("A");
// Add element at the end
list.add("E");
System.out.println("Updated LinkedList: " + list);
}
}
Output
Original LinkedList: [B, C, D] Updated LinkedList: [A, B, C, D, E]
Explanation
The addFirst() method inserts "A" at index 0, while add("E") appends "E" to the end of the list.
Note: add() and addLast() both add an element at the end of a LinkedList.
3. Using add(index, element) Method
The add(index, element) method can be used when we want to insert an element at a particular position.
- To insert an element at the beginning, use index 0.
- To insert an element at the end, use the current size of the list as the index.
Syntax:
list.add(index, element);
Approach
- Create a LinkedList.
- Add some elements to the list.
- Use index 0 to insert an element at the beginning.
- Use list.size() to insert an element at the end.
- Print the updated list.
import java.util.LinkedList;
public class AddFirstLast {
public static void main(String[] args)
{
LinkedList<Integer> list = new LinkedList<>();
list.add(20);
list.add(30);
list.add(40);
System.out.println("Original LinkedList: " + list);
// Insert at the first position
list.add(0, 10);
// Insert at the last position
list.add(list.size(), 50);
System.out.println("Updated LinkedList: " + list);
}
}
Output
Original LinkedList: [20, 30, 40] Updated LinkedList: [10, 20, 30, 40, 50]
Explanation: The program uses add(index, element) to insert elements at specific positions in the LinkedList.
list.add(0, 10) inserts 10 at the beginning, while list.add(list.size(), 50) inserts 50 at the end of the list.
Difference Between add(), addFirst() and addLast()
| Method | Purpose | Position |
|---|---|---|
add() | Adds an element to the list | End of the list |
addFirst() | Adds an element at the beginning | First position |
addLast() | Adds an element at the end | Last position |
add(index, element) | Adds an element at a specific position | Given index |