From 6c50d79952d694df2978d646f81008d3a12cbadb Mon Sep 17 00:00:00 2001 From: Alejandro Quivera <35770411+Alequi@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:49:24 +0200 Subject: [PATCH 1/7] Add concept entry for LinkedList --- .../java/concepts/linked-list/linked-list.md | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 content/java/concepts/linked-list/linked-list.md 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..bef86b01de4 --- /dev/null +++ b/content/java/concepts/linked-list/linked-list.md @@ -0,0 +1,85 @@ +# LinkedList + +A `LinkedList` is a **doubly-linked list** implementation of the `List` and `Deque` interfaces. +- Elements are stored in nodes linked to previous and next nodes. +- Allows `null` elements. +- Useful for queues, stacks, and lists with frequent insertions/removals at the ends. + +--- + +## Syntax + +**Creation:** +```java +LinkedList list = new LinkedList<>(); +``` + +**Common methods:** + +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: + +- `boolean add(E e)` : Appends the specified element to the end of the list (equivalent to `addLast(E)`); returns true if the list changed as a result. For example, `list.add("X")` adds "X" to the tail of the list. +- `void add(int index, E element)` : Inserts the given element at the specified position in the list (shifting subsequent elements to the right). For example, `list.add(0, "Y")` inserts "Y" at the front (index 0). +- `void addFirst(E e)` : Inserts the specified element at the beginning of the list (makes it the new head). After `list.addFirst("Z")`, "Z" becomes the first element. +- `void addLast(E e)` : Adds element at the end. +- `E get(int index)` : Returns the element at the specified position in the list. For instance, `list.get(2)` retrieves the element at index 2. +- `E getFirst()` / `E getLast()` : First or last element. +- `E remove(int index)` : Removes the element at the specified position in the list and returns it. The list size shrinks and elements to the right of the removed element shift left. +- `E removeFirst()`: Removes and returns the first element of the list (equivalent to removing the head) +- `E removeLast()` : Removes and returns the last element of the list (removes the tail of the list) +- `boolean contains(Object o)` : Checks if element exists. +- `int size()` : Number of elements. + +--- + +## Example + +Let’s demonstrate how to use a `LinkedList in Java`. In the example below, we create a `LinkedList` of strings and perform various operations: Adding elements (at the end and at the beginning), accessing elements by index, and removing elements from the list. We also print the list to see the changes: + +```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); + // Output: List after additions: [Zara, Alice, Bob, Charlie] + + // 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); + // Output: 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**. \ No newline at end of file From 610cd850dd892243a51cd53c9040365c94844de9 Mon Sep 17 00:00:00 2001 From: Alejandro Quivera <35770411+Alequi@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:57:57 +0200 Subject: [PATCH 2/7] Added Metadata Information --- content/java/concepts/linked-list/linked-list.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/content/java/concepts/linked-list/linked-list.md b/content/java/concepts/linked-list/linked-list.md index bef86b01de4..48cade68525 100644 --- a/content/java/concepts/linked-list/linked-list.md +++ b/content/java/concepts/linked-list/linked-list.md @@ -1,3 +1,19 @@ +--- +Title: 'LinkedList' +Description: 'A LinkedList is a doubly-linked list implementation of the List and Deque interfaces.' +Subjects: + - 'Computer Science' +Tags: + - 'Collections' + - 'Data Structures' + - 'Interface' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + + + # LinkedList A `LinkedList` is a **doubly-linked list** implementation of the `List` and `Deque` interfaces. From 89ece261ccc9a6c1975c77e8f204b582ddacd81c Mon Sep 17 00:00:00 2001 From: Alejandro Quivera <35770411+Alequi@users.noreply.github.com> Date: Sat, 6 Sep 2025 15:58:16 +0200 Subject: [PATCH 3/7] Syntax to be wrapped in pseudo block Co-authored-by: Mamta Wardhani --- content/java/concepts/linked-list/linked-list.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/java/concepts/linked-list/linked-list.md b/content/java/concepts/linked-list/linked-list.md index 48cade68525..dd4aea7f899 100644 --- a/content/java/concepts/linked-list/linked-list.md +++ b/content/java/concepts/linked-list/linked-list.md @@ -26,7 +26,7 @@ A `LinkedList` is a **doubly-linked list** implementation of the `List` and `Deq ## Syntax **Creation:** -```java +```pseudo LinkedList list = new LinkedList<>(); ``` From 1c6383400bb310606c017431b0df1598a2787878 Mon Sep 17 00:00:00 2001 From: Alejandro Quivera <35770411+Alequi@users.noreply.github.com> Date: Sat, 6 Sep 2025 16:02:15 +0200 Subject: [PATCH 4/7] Line +17 to +22 into paragraph format, instead of bullets --- content/java/concepts/linked-list/linked-list.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/content/java/concepts/linked-list/linked-list.md b/content/java/concepts/linked-list/linked-list.md index dd4aea7f899..1474f21dffe 100644 --- a/content/java/concepts/linked-list/linked-list.md +++ b/content/java/concepts/linked-list/linked-list.md @@ -14,12 +14,8 @@ CatalogContent: -# LinkedList +A `LinkedList` is a **doubly-linked list** implementation of the `List` and `Deque` interfaces. Elements are stored in nodes linked to previous and next nodes, it allows `null` elements and is useful for queues, stacks, and lists with frequent insertions or removals at the ends. -A `LinkedList` is a **doubly-linked list** implementation of the `List` and `Deque` interfaces. -- Elements are stored in nodes linked to previous and next nodes. -- Allows `null` elements. -- Useful for queues, stacks, and lists with frequent insertions/removals at the ends. --- From a98deaac5f52983eeb1d9abd4b2ea7e9a6fb2933 Mon Sep 17 00:00:00 2001 From: Alejandro Quivera <35770411+Alequi@users.noreply.github.com> Date: Sat, 6 Sep 2025 16:06:33 +0200 Subject: [PATCH 5/7] Parameters and return values after sintax --- content/java/concepts/linked-list/linked-list.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/content/java/concepts/linked-list/linked-list.md b/content/java/concepts/linked-list/linked-list.md index 1474f21dffe..3e0b283f76f 100644 --- a/content/java/concepts/linked-list/linked-list.md +++ b/content/java/concepts/linked-list/linked-list.md @@ -19,13 +19,18 @@ A `LinkedList` is a **doubly-linked list** implementation of the `List` and `Deq --- -## Syntax - **Creation:** ```pseudo LinkedList list = new LinkedList<>(); ``` +**Parameters:** +- `Type` → The data type of elements stored in the list (e.g., `String`, `Integer`). + +**Return value:** +- A new empty `LinkedList` object of the specified type. +``` + **Common methods:** 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: From 93f1da57fca220d534d4540fa7c38c00e0ade7fa Mon Sep 17 00:00:00 2001 From: Alejandro Quivera <35770411+Alequi@users.noreply.github.com> Date: Sat, 6 Sep 2025 16:08:37 +0200 Subject: [PATCH 6/7] lines +33 to +47 into tabular format --- .../java/concepts/linked-list/linked-list.md | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/content/java/concepts/linked-list/linked-list.md b/content/java/concepts/linked-list/linked-list.md index 3e0b283f76f..e8d4ec24c44 100644 --- a/content/java/concepts/linked-list/linked-list.md +++ b/content/java/concepts/linked-list/linked-list.md @@ -35,18 +35,19 @@ LinkedList list = new LinkedList<>(); 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: -- `boolean add(E e)` : Appends the specified element to the end of the list (equivalent to `addLast(E)`); returns true if the list changed as a result. For example, `list.add("X")` adds "X" to the tail of the list. -- `void add(int index, E element)` : Inserts the given element at the specified position in the list (shifting subsequent elements to the right). For example, `list.add(0, "Y")` inserts "Y" at the front (index 0). -- `void addFirst(E e)` : Inserts the specified element at the beginning of the list (makes it the new head). After `list.addFirst("Z")`, "Z" becomes the first element. -- `void addLast(E e)` : Adds element at the end. -- `E get(int index)` : Returns the element at the specified position in the list. For instance, `list.get(2)` retrieves the element at index 2. -- `E getFirst()` / `E getLast()` : First or last element. -- `E remove(int index)` : Removes the element at the specified position in the list and returns it. The list size shrinks and elements to the right of the removed element shift left. -- `E removeFirst()`: Removes and returns the first element of the list (equivalent to removing the head) -- `E removeLast()` : Removes and returns the last element of the list (removes the tail of the list) -- `boolean contains(Object o)` : Checks if element exists. -- `int size()` : Number of elements. - +| 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()` | Removes and returns the first element (head). | `list.removeFirst()` | +| `E removeLast()` | Removes 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 number of elements in the list. | `list.size()` | --- ## Example From e30bdb18f7092e2a5491c687a701e459cd016e46 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 16 Sep 2025 10:57:56 +0530 Subject: [PATCH 7/7] Looks good for a second review! --- .../java/concepts/linked-list/linked-list.md | 97 ++++++++++--------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/content/java/concepts/linked-list/linked-list.md b/content/java/concepts/linked-list/linked-list.md index e8d4ec24c44..a931cb66da0 100644 --- a/content/java/concepts/linked-list/linked-list.md +++ b/content/java/concepts/linked-list/linked-list.md @@ -1,7 +1,8 @@ --- Title: 'LinkedList' -Description: 'A LinkedList is a doubly-linked list implementation of the List and Deque interfaces.' +Description: 'A LinkedList in Java is a doubly-linked list implementation of the List and Deque interfaces.' Subjects: + - 'Code Foundations' - 'Computer Science' Tags: - 'Collections' @@ -12,26 +13,23 @@ CatalogContent: - 'paths/computer-science' --- +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. It provides efficient insertions and removals at the list ends and implements standard deque/queue operations (push/pop, offer/poll, addFirst/addLast). +## Syntax -A `LinkedList` is a **doubly-linked list** implementation of the `List` and `Deque` interfaces. Elements are stored in nodes linked to previous and next nodes, it allows `null` elements and is useful for queues, stacks, and lists with frequent insertions or removals at the ends. - - ---- - -**Creation:** ```pseudo LinkedList list = new LinkedList<>(); ``` -**Parameters:** -- `Type` → The data type of elements stored in the list (e.g., `String`, `Integer`). +**Parameters:** -**Return value:** -- A new empty `LinkedList` object of the specified type. -``` +- `Type`: The data type of elements stored in the list (eg., `String`, `Integer`). + +**Return value:** -**Common methods:** +- 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: @@ -48,56 +46,61 @@ Common methods of `LinkedList` (inherited from List/Deque or defined in the clas | `E removeLast()` | Removes 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 number of elements in the list. | `list.size()` | ---- ## Example -Let’s demonstrate how to use a `LinkedList in Java`. In the example below, we create a `LinkedList` of strings and perform various operations: Adding elements (at the end and at the beginning), accessing elements by index, and removing elements from the list. We also print the list to see the changes: +In the example below, we create a `LinkedList` of strings and perform various operations: Adding elements (at the end and at the beginning), accessing elements by index, and removing elements from the list. We also print the list to see the changes: ```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); - // Output: List after additions: [Zara, Alice, Bob, Charlie] - - // 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); - // Output: List after removals: [Bob] - } + 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 +## `LinkedList` vs `ArrayList` -| Feature | LinkedList | 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**. \ No newline at end of file +**Rule of thumb:** + +- Use `ArrayList` for most cases (better performance & memory). +- Use `LinkedList` when you need frequent insertions/removals at the beginning or end.