Data Insert
## Java LinkedList: Inserting Elements at the Beginning and End
In Java, the `java.util.LinkedList` class provides a doubly-linked list implementation of the `List` and `Deque` interfaces. Because of its underlying doubly-linked list structure, inserting elements at either the beginning or the end of the list is highly efficient, operating in constant time ($O(1)$ complexity).
This tutorial demonstrates how to use the `addFirst()` and `addLast()` methods of the `LinkedList` class to insert elements at the head and tail of a list.
---
### Method Signatures and Syntax
The `LinkedList` class implements the `Deque` (double-ended queue) interface, which exposes the following methods for element insertion:
#### 1. `addFirst(E e)`
Inserts the specified element at the beginning of this list.
* **Syntax:** `public void addFirst(E e)`
* **Parameter:** `e` - The element to add.
* **Returns:** `void`
#### 2. `addLast(E e)`
Appends the specified element to the end of this list. (This is functionally equivalent to the standard `add(E e)` method).
* **Syntax:** `public void addLast(E e)`
* **Parameter:** `e` - The element to add.
* **Returns:** `void`
---
### Code Example
The following complete Java program demonstrates how to initialize a `LinkedList`, populate it with initial values, and then use `addFirst()` and `addLast()` to prepend and append elements.
```java
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
// Initialize a LinkedList of Strings
LinkedList lList = new LinkedList();
// Populate the list with initial elements
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
// Display the initial list
System.out.println("Initial List: " + lList);
// Insert an element at the beginning (index 0)
lList.addFirst("0");
System.out.println("After addFirst(\"0\"): " + lList);
// Insert an element at the end
lList.addLast("6");
System.out.println("After addLast(\"6\"): " + lList);
}
}
```
### Output
When you run the program above, it produces the following output:
```text
Initial List: [1, 2, 3, 4, 5]
After addFirst("0"): [0, 1, 2, 3, 4, 5]
After addLast("6"): [0, 1, 2, 3, 4, 5, 6]
```
---
### Key Considerations and Best Practices
* **Performance ($O(1)$ Complexity):** Unlike an `ArrayList`, where inserting an element at the beginning requires shifting all subsequent elements in memory ($O(n)$ complexity), `LinkedList` performs `addFirst()` and `addLast()` operations in $O(1)$ constant time. This makes `LinkedList` an excellent choice for implementing Queues, Stacks, or Deques.
* **Alternative Methods:**
* For prepending elements, you can also use `offerFirst(E e)`, which is similar to `addFirst(E e)` but is designed to comply with capacity-restricted queues.
* For appending elements, the standard `add(E e)` or `offerLast(E e)` can be used interchangeably with `addLast(E e)`.
* **Null Elements:** `LinkedList` allows `null` elements. However, exercise caution when inserting `null` values if your application logic relies on non-null checks.
* **Thread Safety:** `LinkedList` is **not synchronized**. If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally (such as adding elements), it must be synchronized externally (e.g., using `Collections.synchronizedList`).
YouTip