Commit b9bed14
committed
feat: add LinkedList demo and manual Node implementation
What
- Added `Demo` class to show usage of Java’s built-in `LinkedList<Integer>`.
- Added `Test` class with custom `Node` class to illustrate how a linked list works internally.
- Created two nodes manually, linked them together (`linkedlistNode → linkedlistNode2 → null`).
Why
- Demonstrates two perspectives:
1. High-level usage of `LinkedList` from Java Collections Framework.
2. Low-level mechanics of a singly linked list with explicit nodes and pointers.
- Helps understand what happens “behind the scenes” when using `LinkedList`.
How
- In `Demo`:
- Declared `LinkedList<Integer> AddToLinkedList`.
- Added integers (1, 2, 4, 3).
- Printed list: uses `toString()` to display `[1, 2, 4, 3]`.
- In `Test`:
- Declared two `Node` objects.
- Assigned values `2` and `1`.
- Linked first node to second with `linkedlistNode.next = linkedlistNode2`.
- Set `linkedlistNode2.next = null` to terminate list.
Logic
- `LinkedList` (Java class): doubly linked list, each node stores references to both previous and next nodes.
- `Node` (custom class): simplified singly linked list node (value + next).
- Flow in `Test`:
1. Create two `Node` objects.
2. Assign values.
3. Link them together.
4. End chain with `null`.
Complexity
- For Java `LinkedList`:
- Insertion/removal at head/tail: O(1).
- Access by index: O(n).
- For manual `Node` implementation:
- Same as a classic singly linked list:
- Insert at head: O(1).
- Insert/search arbitrary: O(n).
Applications
- Good for problems requiring frequent insertions/removals in middle of a list.
- Demonstrates underlying concept of dynamic memory usage (nodes connected via references).
- Useful as building block for stacks, queues, and graph adjacency lists.
Notes
- Java’s built-in `LinkedList` is doubly-linked (supports bidirectional traversal).
- The custom `Node` class is singly-linked, simpler but less flexible.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent ed5616f commit b9bed14
2 files changed
+36
-0
lines changedLines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
Lines changed: 21 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
0 commit comments