Skip to content

Commit b9bed14

Browse files
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

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Package;
2+
3+
import java.util.LinkedList;
4+
5+
public class Demo {
6+
public static void main(String[] args) {
7+
LinkedList<Integer> AddToLinkedList = new LinkedList<>();
8+
AddToLinkedList.add(1);
9+
AddToLinkedList.add(2);
10+
AddToLinkedList.add(4);
11+
AddToLinkedList.add(3);
12+
13+
System.out.println(AddToLinkedList);
14+
}
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Package;
2+
3+
public class Test {
4+
public static void main(String[] args) {
5+
// Behind the view of working a Linked list in Java.
6+
Node linkedlistNode = new Node();
7+
Node linkedlistNode2 = new Node();
8+
9+
linkedlistNode.value = 2;
10+
linkedlistNode2.value = 1;
11+
12+
linkedlistNode.next = linkedlistNode2;
13+
linkedlistNode2.next = null;
14+
15+
}
16+
}
17+
18+
class Node {
19+
public int value;
20+
public Node next; // Reference to what ...Next Node
21+
}

0 commit comments

Comments
 (0)