Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

707. Design Linked List

Medium28.6% Acceptance
Linked ListDesign
Asked by:
A
Amazon
Google
ProblemSolutions (12)VideosCompanies (5)Notes

Problem Statement

Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement the MyLinkedList class:

  • MyLinkedList() Initializes the MyLinkedList object.
  • int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1.
  • void addAtHead(int val) Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
  • void addAtTail(int val) Append a node of value val as the last element of the linked list.
  • void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.
  • void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.

Example 1:

Input
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
[[], [1], [3], [1, 2], [1], [1], [1]]
Output
[null, null, null, null, 2, null, 3]

Explanation
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2);    // linked list becomes 1->2->3
myLinkedList.get(1);              // return 2
myLinkedList.deleteAtIndex(1);    // now the linked list is 1->3
myLinkedList.get(1);              // return 3

Constraints:

  • 0 <= index, val <= 1000
  • Please do not use the built-in LinkedList library.
  • At most 2000 calls will be made to get, addAtHead, addAtTail, addAtIndex and deleteAtIndex.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
G
M
Microsoft
N
Nvidia
V
Virtu Financial

Approach

In #707 Design Linked List, you must implement a linked list from scratch and support operations such as get, addAtHead, addAtTail, addAtIndex, and deleteAtIndex. The key idea is to manage node connections efficiently while tracking the current size of the list.

A common approach is to build a doubly linked list with sentinel (dummy) head and tail nodes. These dummy nodes simplify edge cases when inserting or deleting at the beginning or end. Each node stores its value and pointers to the previous and next nodes. When performing operations at an index, traverse from the head (or tail if optimized) until reaching the desired position.

Maintaining a size variable helps quickly validate indices and avoid unnecessary traversal. Most updates like adding at head or tail can be done in constant time, while operations involving arbitrary indices require traversal.

The overall design emphasizes clean pointer manipulation and careful boundary handling to maintain correct list structure.

Complexity

OperationTime ComplexitySpace Complexity
Get by IndexO(n)O(1)
Add at Head / TailO(1)O(1)
Add at IndexO(n)O(1)
Delete at IndexO(n)O(1)

Video Solution Available

Ashish Pratap Singh

View all video solutions

Solutions (12)

Singly Linked List Implementation

In this approach, you will design a singly linked list where each node has a value and a pointer to the next node. This type of list is simple and efficient for certain operations, especially when traversing only forwards is sufficient.

Time Complexity:
- get: O(n)
- addAtHead: O(1)
- addAtTail: O(n)
- addAtIndex: O(n)
- deleteAtIndex: O(n)
Space Complexity: O(n), where n is the number of nodes in the list.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4struct Node {
5    int val;
6    struct Node*







Explanation

The above code implements a singly linked list in C. It provides functions to add nodes at the head, tail, a specific index, get the value at an index, and delete a node at an index. The linked list maintains a head pointer and nodes are dynamically allocated using malloc.

Doubly Linked List Implementation

This approach designs a doubly linked list, allowing traversal both forwards and backwards. Each node maintains a reference to both the next and previous node, facilitating operations such as adding or removing nodes from either end more efficiently.

Time Complexity:
- get: O(n)
- addAtHead: O(1)
- addAtTail: O(n)
- addAtIndex: O(n)
- deleteAtIndex: O(n)
Space Complexity: O(n), where n is the number of nodes in the list.

CC++JavaPythonC#JavaScript
1








Video Solutions

Watch expert explanations and walkthroughs

LeetCode was HARD until I Learned these 15 Patterns

Ashish Pratap Singh
13:001,002,140 views

Asked By Companies

5 companies
A
Amazon
G
Google
M
Microsoft
N
Nvidia
V
Virtu Financial

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Related Topics

Linked ListDesign

Problem Stats

Acceptance Rate28.6%
DifficultyMedium
Companies5

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why use dummy nodes in Design Linked List?

Dummy or sentinel nodes help avoid special cases when inserting or deleting at the beginning or end of the list. They provide stable boundary nodes so pointer updates become consistent across all operations.

Is Design Linked List asked in FAANG interviews?

Yes, linked list design and manipulation problems are common in FAANG-style interviews. They test understanding of pointers, data structure implementation, and handling edge cases in low-level operations.

What data structure is best for Design Linked List?

A doubly linked list is usually the best structure for this problem because it allows efficient insertion and deletion with access to both previous and next nodes. Dummy nodes further simplify operations at the head and tail.

What is the optimal approach for Design Linked List?

The optimal approach is to implement a doubly linked list with sentinel head and tail nodes. This design simplifies insertion and deletion at boundaries and reduces edge-case handling. Maintaining a size variable also helps validate indices efficiently.

Previous Problem

Design HashMap

Next Problem

Insert into a Sorted Circular Linked List

next
;
7
}
;
8
9
struct
MyLinkedList
{
10
struct
Node
*
head
;
11
}
;
12
13
struct
MyLinkedList
*
myLinkedListCreate
(
)
{
14
struct
MyLinkedList
*
obj
=
(
struct
MyLinkedList
*
)
malloc
(
sizeof
(
struct
MyLinkedList
)
)
;
15
obj
->
head
=
NULL
;
16
return
obj
;
17
}
18
19
void
myLinkedListAddAtHead
(
struct
MyLinkedList
*
obj
,
int
val
)
{
20
struct
Node
*
node
=
(
struct
Node
*
)
malloc
(
sizeof
(
struct
Node
)
)
;
21
node
->
val
=
val
;
22
node
->
next
=
obj
->
head
;
23
obj
->
head
=
node
;
24
}
25
26
void
myLinkedListAddAtTail
(
struct
MyLinkedList
*
obj
,
int
val
)
{
27
struct
Node
*
node
=
(
struct
Node
*
)
malloc
(
sizeof
(
struct
Node
)
)
;
28
node
->
val
=
val
;
29
node
->
next
=
NULL
;
30
if
(
obj
->
head
==
NULL
)
{
31
obj
->
head
=
node
;
32
}
else
{
33
struct
Node
*
current
=
obj
->
head
;
34
while
(
current
->
next
!=
NULL
)
{
35
current
=
current
->
next
;
36
}
37
current
->
next
=
node
;
38
}
39
}
40
41
int
myLinkedListGet
(
struct
MyLinkedList
*
obj
,
int
index
)
{
42
struct
Node
*
current
=
obj
->
head
;
43
int
i
=
0
;
44
while
(
current
!=
NULL
&&
i
<
index
)
{
45
current
=
current
->
next
;
46
i
++
;
47
}
48
return
(
current
==
NULL
)
?
-
1
:
current
->
val
;
49
}
50
51
void
myLinkedListAddAtIndex
(
struct
MyLinkedList
*
obj
,
int
index
,
int
val
)
{
52
if
(
index
==
0
)
{
53
myLinkedListAddAtHead
(
obj
,
val
)
;
54
return
;
55
}
56
struct
Node
*
current
=
obj
->
head
;
57
for
(
int
i
=
0
;
current
!=
NULL
&&
i
<
index
-
1
;
i
++
)
{
58
current
=
current
->
next
;
59
}
60
if
(
current
==
NULL
)
return
;
61
struct
Node
*
node
=
(
struct
Node
*
)
malloc
(
sizeof
(
struct
Node
)
)
;
62
node
->
val
=
val
;
63
node
->
next
=
current
->
next
;
64
current
->
next
=
node
;
65
}
66
67
void
myLinkedListDeleteAtIndex
(
struct
MyLinkedList
*
obj
,
int
index
)
{
68
struct
Node
*
current
=
obj
->
head
;
69
if
(
index
==
0
&&
current
!=
NULL
)
{
70
obj
->
head
=
current
->
next
;
71
free
(
current
)
;
72
return
;
73
}
74
struct
Node
*
prev
=
NULL
;
75
for
(
int
i
=
0
;
current
!=
NULL
&&
i
<
index
;
i
++
)
{
76
prev
=
current
;
77
current
=
current
->
next
;
78
}
79
if
(
current
==
NULL
)
return
;
80
prev
->
next
=
current
->
next
;
81
free
(
current
)
;
82
}
83
84
void
myLinkedListFree
(
struct
MyLinkedList
*
obj
)
{
85
struct
Node
*
current
=
obj
->
head
;
86
while
(
current
!=
NULL
)
{
87
struct
Node
*
next
=
current
->
next
;
88
free
(
current
)
;
89
current
=
next
;
90
}
91
free
(
obj
)
;
92
}
#
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
struct
Node
{
5
int
val
;
6
struct
Node
*
next
;
7
struct
Node
*
prev
;
8
}
;
9
10
struct
MyLinkedList
{
11
struct
Node
*
head
;
12
}
;
13
14
struct
MyLinkedList
*
myLinkedListCreate
(
)
{
15
struct
MyLinkedList
*
obj
=
(
struct
MyLinkedList
*
)
malloc
(
sizeof
(
struct
MyLinkedList
)
)
;
16
obj
->
head
=
NULL
;
17
return
obj
;
18
}
19
20
void
myLinkedListAddAtHead
(
struct
MyLinkedList
*
obj
,
int
val
)
{
21
struct
Node
*
node
=
(
struct
Node
*
)
malloc
(
sizeof
(
struct
Node
)
)
;
22
node
->
val
=
val
;
23
node
->
next
=
obj
->
head
;
24
node
->
prev
=
NULL
;
25
if
(
obj
->
head
!=
NULL
)
{
26
obj
->
head
->
prev
=
node
;
27
}
28
obj
->
head
=
node
;
29
}
30
31
void
myLinkedListAddAtTail
(
struct
MyLinkedList
*
obj
,
int
val
)
{
32
struct
Node
*
node
=
(
struct
Node
*
)
malloc
(
sizeof
(
struct
Node
)
)
;
33
node
->
val
=
val
;
34
node
->
next
=
NULL
;
35
if
(
obj
->
head
==
NULL
)
{
36
node
->
prev
=
NULL
;
37
obj
->
head
=
node
;
38
}
else
{
39
struct
Node
*
current
=
obj
->
head
;
40
while
(
current
->
next
!=
NULL
)
{
41
current
=
current
->
next
;
42
}
43
current
->
next
=
node
;
44
node
->
prev
=
current
;
45
}
46
}
47
48
int
myLinkedListGet
(
struct
MyLinkedList
*
obj
,
int
index
)
{
49
struct
Node
*
current
=
obj
->
head
;
50
int
i
=
0
;
51
while
(
current
!=
NULL
&&
i
<
index
)
{
52
current
=
current
->
next
;
53
i
++
;
54
}
55
return
(
current
==
NULL
)
?
-
1
:
current
->
val
;
56
}
57
58
void
myLinkedListAddAtIndex
(
struct
MyLinkedList
*
obj
,
int
index
,
int
val
)
{
59
if
(
index
==
0
)
{
60
myLinkedListAddAtHead
(
obj
,
val
)
;
61
return
;
62
}
63
struct
Node
*
current
=
obj
->
head
;
64
for
(
int
i
=
0
;
current
!=
NULL
&&
i
<
index
-
1
;
i
++
)
{
65
current
=
current
->
next
;
66
}
67
if
(
current
==
NULL
)
return
;
68
struct
Node
*
node
=
(
struct
Node
*
)
malloc
(
sizeof
(
struct
Node
)
)
;
69
node
->
val
=
val
;
70
node
->
next
=
current
->
next
;
71
if
(
current
->
next
!=
NULL
)
{
72
current
->
next
->
prev
=
node
;
73
}
74
current
->
next
=
node
;
75
node
->
prev
=
current
;
76
}
77
78
void
myLinkedListDeleteAtIndex
(
struct
MyLinkedList
*
obj
,
int
index
)
{
79
struct
Node
*
current
=
obj
->
head
;
80
int
i
;
81
for
(
i
=
0
;
current
!=
NULL
&&
i
<
index
;
i
++
)
{
82
current
=
current
->
next
;
83
}
84
if
(
current
==
NULL
)
return
;
85
if
(
current
->
prev
!=
NULL
)
{
86
current
->
prev
->
next
=
current
->
next
;
87
}
else
{
88
obj
->
head
=
current
->
next
;
89
}
90
if
(
current
->
next
!=
NULL
)
{
91
current
->
next
->
prev
=
current
->
prev
;
92
}
93
free
(
current
)
;
94
}
95
96
void
myLinkedListFree
(
struct
MyLinkedList
*
obj
)
{
97
struct
Node
*
current
=
obj
->
head
;
98
while
(
current
!=
NULL
)
{
99
struct
Node
*
next
=
current
->
next
;
100
free
(
current
)
;
101
current
=
next
;
102
}
103
free
(
obj
)
;
104
}

Explanation

The C implementation of a doubly linked list adds a prev pointer to each node, enabling two-way node traversal and efficient deletion and insertion operations from both ends.

LRU CacheMedium
Design TwitterMedium
Design Phone DirectoryMedium
All O`one Data StructureHard
More similar problems