Design a linked list data structure that supports indexed insert, delete, read, and prepend/append operations.
Design Linked List
gfgDesign a Linked List
Implement a custom linked list with the following operations:
get(index): return the value at the given indexaddAtHead(val): insert a node with valuevalat the frontaddAtTail(val): insert a node with valuevalat the endaddAtIndex(index, val): insert a node with valuevalbefore the node currently atindexdeleteAtIndex(index): delete the node atindex
The list is initially empty.
Your implementation should maintain correct node links and handle out-of-range indices safely.
Notes
- Indices are zero-based.
- If
indexis invalid for a read or delete operation, return a safe failure value or do nothing as appropriate. - Insertions at
index == sizeare valid and append to the end. - Negative indices are invalid.
Input Format
The data structure is used through a sequence of method calls:
get(index)addAtHead(val)addAtTail(val)addAtIndex(index, val)deleteAtIndex(index)
Each call provides integer arguments.
Output Format
get(index)returns the value at the requested position, or-1if the index is invalid.- Update operations modify the linked list in place and do not return a value.
Constraints
- number of operations large
- Values and indices are integers
- Indexing is zero-based
- Invalid insert/delete positions should be ignored
Because this is a design problem, exact platform constraints may vary by version.
Example 1
Input
[
Output
-
Explanation
A complete interactive example is omitted here because the problem is usually judged by method calls rather than a single input/output pair.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.