-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[Concept Entry] Java : LinkedList #7543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Alequi
wants to merge
9
commits into
Codecademy:main
Choose a base branch
from
Alequi:java-linkedlist
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6c50d79
Add concept entry for LinkedList
Alequi 610cd85
Added Metadata Information
Alequi b695eae
Merge branch 'main' into java-linkedlist
Alequi 89ece26
Syntax to be wrapped in pseudo block
Alequi 1c63834
Line +17 to +22 into paragraph format, instead of bullets
Alequi a98deaa
Parameters and return values after sintax
Alequi 93f1da5
lines +33 to +47 into tabular format
Alequi e30bdb1
Looks good for a second review!
mamtawardhani 32c57eb
Merge branch 'main' into java-linkedlist
mamtawardhani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
--- | ||
|
||
A **`LinkedList`** is a **doubly-linked list** implementation in `java.util` that implements `List<E>`, `Deque<E>`, `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 | ||
|
||
```pseudo | ||
LinkedList<Type> 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()` | 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 | ||
|
||
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<String> 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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.