Skip to content

Practice Design Linked List

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#707
LeetCode Problem

Design Linked List

Problem Statement

MyLinkedList starts empty and uses zero-based indices. get(index) returns a node value or -1 for an invalid index. addAtHead(val) and addAtTail(val) insert at the ends. addAtIndex(index,val) inserts before that position, appends at length, and does nothing beyond length. deleteAtIndex(index) removes only a valid position. Implement nodes yourself rather than using a linked-list library. Practice approach: A sentinel head simplifies insertions and deletions at index zero. Track size for bounds checks and locate the predecessor before changing links. Check empty-list operations and insertion exactly at the tail. A singly linked implementation uses O(n) indexed operations and O(n) storage.

Source: Design Linked List on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["MyLinkedList","addAtHead","addAtTail","addAtIndex","get","deleteAtIndex","get","addAtIndex","get"]
[[],[4],[9],[1,7],[1],[1],[1],[5,2],[2]]
Output
[null,null,null,null,7,null,9,null,-1]
Explanation

Insertion produces [4,7,9], deletion restores [4,9], and insertion beyond its length is ignored.

Constraints

0 <= index, val <= 1000
At most 2000 total method calls.
Do not use a built-in LinkedList library.

Related LeetCode Problems

Practice more problems similar to Design Linked List.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.