Design a Custom Linked List

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2020-11-11

Problem Statement

Design and implement a custom linked list data structure (singly or doubly linked list) that supports insertion, deletion, traversal, and search operations. The implementation should not rely on built-in library data structures.


Functional Requirements (FR)


Node Structure


•Each node should store a value and a pointer/reference to the next node (and previous node if doubly linked list).


Insertion


•Insert at head.
•Insert at tail.
•Insert at a specific index/position.


Deletion


•Delete from head.
•Delete from tail.
•Delete a specific value/index.


Traversal


•Print all elements in order.
•For doubly linked list → allow reverse traversal.


Search


•Find an element by value.
•Return index/position if found.


Utility Operations


•Get size of the list.
•Check if the list is empty.
•Clear all elements.


Non-Functional Requirements (NFR)


Time Complexity:


•Insert/Delete at head → O(1).
•Insert/Delete at tail (singly) → O(n), (doubly) → O(1).
•Search → O(n).
•Memory Usage: Minimal overhead for node storage.
•Extensibility: Should support generic types.


Core Entities & Attributes


•Node<T>
•Attributes: value: T, next: Node<T>, prev: Node<T> (for doubly list).
•LinkedList<T>
•Attributes: head, tail, size.
•Operations:
•insertAtHead(value: T)
•insertAtTail(value: T)
•insertAt(value: T, index: int)
•deleteAt(index: int)
•search(value: T): int
•traverse(): List<T>


Example Use Case


1.Create empty linked list.
2.Insert 10 at head → list = [10].
3.Insert 20 at tail → list = [10 → 20].
4.Insert 15 at index 1 → list = [10 → 15 → 20].
5.Delete index 1 → list = [10 → 20].
6.Search for 20 → returns index 1.


Evaluation Criteria


•Correct class and node design.
•Proper handling of edge cases (insert/delete on empty list, invalid index).
•Clear time complexity awareness.
•Optional: Ability to switch between singly and doubly linked list.


Solution

Comments