✅ What is a LinkedList?
A LinkedList is a linear data structure where each element (called a node) contains:
-
data
-
a reference (link) to the next node in the list.
✅ Node Structure
Each Node
contains:
✅ LinkedList Operations
1. Add (at end) – add(T data)
-
Create a new node.
-
If the list is empty, set
head = newNode
. -
Otherwise, traverse to the last node and set its
next
to the new node.
2. Remove (by value) – remove(T data)
-
Check if head is null → return false.
-
If
head.data == data
, remove it byhead = head.next
. -
Otherwise, traverse until
current.next.data == data
, then skip it bycurrent.next = current.next.next
.
✅ Java Code – Custom LinkedList
✅ Main Method to Test
No comments:
Post a Comment