diff --git a/content/java/concepts/linked-list/linked-list.md b/content/java/concepts/linked-list/linked-list.md new file mode 100644 index 00000000000..c208ffb7a49 --- /dev/null +++ b/content/java/concepts/linked-list/linked-list.md @@ -0,0 +1,106 @@ +--- +Title: 'LinkedList' +Description: 'A LinkedList in Java is a doubly-linked list implementation of the List and Deque interfaces.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Collections' + - 'Data Structures' + - 'Interface' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + +In Java, a **`LinkedList`** is a doubly linked list implementation in `java.util` that implements `List`, `Deque`, `Cloneable`, and `Serializable`. Elements are stored in nodes linked to previous and next nodes. It allows `null` elements, provides efficient insertions and removals at the list ends and implements standard deque/queue operations (push/pop, offer/poll, `addFirst()`/`addLast()`). + +## Syntax + +```pseudo +LinkedList list = new LinkedList<>(); +``` + +**Parameters:** + +- `Type`: The data type of elements stored in the list (eg., `String`, `Integer`). + +**Return value:** + +- A new empty `LinkedList` instance of the specified type. + +## Common Methods in a Linked List + +Common methods of `LinkedList` (inherited from `List`/`Deque` or defined in the class) include those for adding, removing, and accessing elements. Here are some of the most frequently used methods with their signatures and brief descriptions: + +| Method | Description | Example | +| -------------------------------- | ---------------------------------------------------------------------------------------------------------------- | -------------------- | +| `boolean add(E e)` | Appends the specified element to the end of the list (same as `addLast(E)`). Returns `true` if the list changed. | `list.add("X")` | +| `void add(int index, E element)` | Inserts an element at the specified position, shifting subsequent elements. | `list.add(0, "Y")` | +| `void addFirst(E e)` | Inserts the element at the beginning (new head). | `list.addFirst("Z")` | +| `void addLast(E e)` | Adds element at the end of the list. | `list.addLast("A")` | +| `E get(int index)` | Returns the element at the specified position. | `list.get(2)` | +| `E getFirst()` / `E getLast()` | Returns the first or last element. | `list.getFirst()` | +| `E remove(int index)` | Removes and returns the element at the given position. | `list.remove(1)` | +| `E removeFirst()` | Deletes and returns the first element (head). | `list.removeFirst()` | +| `E removeLast()` | Deletes and returns the last element (tail). | `list.removeLast()` | +| `boolean contains(Object o)` | Checks if the list contains the given element. | `list.contains("X")` | +| `int size()` | Returns the size or number of elements in the list. | `list.size()` | + +## Example + +This example creates a `LinkedList` of strings and performs various operations on it: + +```java +import java.util.LinkedList; + +public class LinkedListDemo { + public static void main(String[] args) { + LinkedList list = new LinkedList<>(); + + // Adding elements to the LinkedList + list.add("Alice"); // Append "Alice" to the end + list.add("Bob"); // Append "Bob" to the end + list.addFirst("Zara"); // Insert "Zara" at the beginning + list.addLast("Charlie");// Insert "Charlie" at the end (same as add) + + System.out.println("List after additions: " + list); + + // Accessing elements + String firstElement = list.getFirst(); // Retrieve first element ("Zara") + String thirdElement = list.get(2); // Retrieve element at index 2 ("Bob") + System.out.println("First element: " + firstElement); + System.out.println("Element at index 2: " + thirdElement); + + // Removing elements + list.removeFirst(); // Removes "Zara" (first element) + list.removeLast(); // Removes "Charlie" (last element) + list.remove("Alice"); // Removes the first occurrence of "Alice" + + System.out.println("List after removals: " + list); + } +} +``` + +The output of this code is: + +```shell +List after additions: [Zara, Alice, Bob, Charlie] +First element: Zara +Element at index 2: Bob +List after removals: [Bob] +``` + +## `LinkedList` vs `ArrayList` + +| Feature | `LinkedList` | `ArrayList` | +| ------------------ | ---------------------------------------------- | -------------------------------------- | +| Access by index | O(n) (must traverse nodes) | O(1) (direct access) | +| Insert/remove ends | O(1) | O(n) at front, O(1) at end (amortized) | +| Memory usage | Higher (extra pointers per node) | Lower (contiguous array) | +| Best use case | Queues, stacks, frequent insert/remove at ends | General-purpose, fast random access | + +**Rule of thumb:** + +- Use `ArrayList` for most cases (better performance & memory). +- Use `LinkedList` when you need frequent insertions/removals at the beginning or end.