Linked List Implementation from Scratch

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-02-13

Problem Statement

Design and implement a Linked List data structure from scratch that supports core operations such as insertion, deletion, traversal, and searching — without using any built-in collection libraries.


📋 Problem Description


You are required to design a LinkedList class that models the behavior of a singly or doubly linked list.


Each element in the list should be represented by a Node object, containing a value and a reference to the next (and optionally, previous) node.


Your design should demonstrate object-oriented principles such as encapsulation and modularity.


🧱 Core Requirements


1. Classes to Design


•Node — represents a single node in the list.
•LinkedList — represents the overall linked list and provides API methods.


2. Functional Requirements


Implement the following methods:


•insertAtHead(value) — Add a new node at the beginning.
•insertAtTail(value) — Add a new node at the end.
•insertAtPosition(value, position) — Add a new node at a specific position.
•deleteByValue(value) — Delete the first occurrence of a node by value.
•deleteAtPosition(position) — Delete a node at a specific position.
•search(value) — Find whether a given value exists in the list.
•display() — Print all elements in the list in order.
•getLength() — Return the current length of the list.


(Optional for doubly linked list)


•Support backward traversal using prev pointers.


⚙️ Non-Functional Requirements


•Should handle edge cases like empty list insertions/deletions gracefully.
•Ensure O(1) insertion at head and O(n) insertion/search/deletion in general cases.
•Write clean, modular, and testable code following SOLID principles.
•No use of built-in list, array, or collection classes for core functionality.


🧩 Example Scenario

Input Operations:

1 2insertAtHead(10) 3insertAtTail(20) 4insertAtTail(30) 5insertAtPosition(25, 2) 6deleteByValue(10) 7display()


Output:

1 2Linked List: 20 -> 25 -> 30


🧠 Design Discussion Points


•How would you extend your design to a Doubly Linked List?
•How can you implement a Circular Linked List using the same structure?
•How to ensure memory cleanup and avoid dangling references?
•What test cases would you write to validate your linked list?


✅ Deliverables

•UML diagram showing relationship between Node and LinkedList.
•Implementation code (in Java/Python/C++).
•Test cases demonstrating each operation


Solution

Comments