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.