Commit a8bf550
committed
feat(LinkedListDemo3): showcase advanced LinkedList operations and traversal
What
- Added `LinkedListDemo3` to demonstrate:
- Construction with `add()`, `addAll()`, `set()`, `addFirst()`, `addLast()`.
- Iteration using `forEach` with method reference.
- Retrieval operations: `getLast()`, `peek()`.
- Size retrieval with `size()`.
- Reverse iteration with `descendingIterator()`.
- Added helper `show(int n)` to print each element during forEach.
Why
- Illustrates that `LinkedList` supports both `List` and `Deque` interfaces.
- Highlights methods unique to `Deque` (addFirst, addLast, poll, peek).
- Shows how to traverse in reverse order without manual indexing.
How
1. Created two linked lists (`ll1` empty, `ll2` from `List.of`).
2. Added integers step by step:
- Inserted elements at specific positions with `add` and `addAll`.
- Updated element at index 5 with `set`.
- Pushed elements at front and back (`addFirst`, `addLast`).
3. Traversed with `forEach` to print elements using `show()`.
4. Retrieved last element with `getLast()`.
5. Printed size with `size()`.
6. Used `descendingIterator` to traverse in reverse order.
7. Used `peek()` to retrieve first element without removing it.
Logic
- Initial `ll1 = [ ]`.
- After operations → `[3, 5, 50, 60, 70, 80, 70, 10, 200]`.
- `forEach` prints each element in order.
- `getLast()` → `200`.
- `size()` → `9`.
- Reverse traversal prints → `200 10 70 80 70 60 50 5 3`.
- `peek()` → `3`.
Complexity
- `addFirst()`, `addLast()`, `peek()`, `poll()` → O(1).
- `add(index, elem)`, `set(index, elem)` → O(n) because of traversal.
- Iteration (normal or descending) → O(n).
Applications
- LinkedList is well-suited for:
- Queue/Deque operations (FIFO/LIFO).
- Scenarios needing frequent insert/remove at both ends.
- Reverse traversal without manually reversing the list.
- Not ideal for random access due to O(n) `get(index)`.
Notes
- `peek()` is safer than `getFirst()`; returns `null` if list is empty instead of throwing `NoSuchElementException`.
- `descendingIterator()` provides clean reverse iteration, unlike ArrayList which requires manual indexing.
Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>1 parent 0a18444 commit a8bf550
1 file changed
Lines changed: 8 additions & 10 deletions
Lines changed: 8 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | | - | |
| 3 | + | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
| 9 | + | |
11 | 10 | | |
12 | 11 | | |
13 | | - | |
| 12 | + | |
14 | 13 | | |
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
21 | | - | |
| 20 | + | |
22 | 21 | | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
28 | | - | |
29 | | - | |
| 27 | + | |
30 | 28 | | |
31 | 29 | | |
32 | 30 | | |
33 | 31 | | |
34 | | - | |
35 | | - | |
| 32 | + | |
| 33 | + | |
36 | 34 | | |
37 | 35 | | |
38 | | - | |
| 36 | + | |
0 commit comments