Linked Lists Singly linked list basics
274. Design Linked List
Build your own linked list of integers. Write a class MyLinkedList with a constructor and five methods.
Positions in the list are counted from 0, so the head is at index 0.
MyLinkedList()creates an empty list.get(index)returns the value of the node atindex, or-1if the list has no node there.addAtHead(val)adds a node holdingvalin front of the first node, so it becomes the new head.addAtTail(val)adds a node holdingvalafter the last node.addAtIndex(index, val)adds a node holdingvaljust before the node atindex, so the new node ends up atindex. Ifindexequals the length of the list, the node goes at the end. Ifindexis greater than the length, nothing happens.deleteAtIndex(index)removes the node atindex. If the list has no node there, nothing happens.
Keep the values in nodes you define yourself, each linked to the next one (and to the one before it, if you like). Do not hold them in a library list or an array. The judge cannot check this rule, so keeping it is up to you.
Your class is tested by replaying a list of calls on a new, empty MyLinkedList. ops[i] names the i-th
call. The calls take their arguments from args in order: get and deleteAtIndex take an index,
addAtHead and addAtTail take a value, and addAtIndex takes an index and then a value. The answer is the
list of every value that get returns, in the order of the calls.
In C# and Go the methods are Get, AddAtHead, AddAtTail, AddAtIndex and DeleteAtIndex, and in Rust
get, add_at_head, add_at_tail, add_at_index and delete_at_index. Go creates the list with
Constructor(), and Rust with MyLinkedList::new(). The names in ops are the same in every language.
Example 1
Input: ops = ["addAtTail",
"addAtTail",
"addAtHead",
"addAtIndex",
"get",
"deleteAtIndex",
"get",
"get"]
args = [4, 9, 2, 1, 7, 1, 1, 1, 3]
[7,4,-1]Explanation: The first three calls build 2 → 4 → 9. addAtIndex(1, 7) puts 7 at index 1, giving 2 → 7 → 4 → 9, so
get(1) is 7. deleteAtIndex(1) removes it again, and get(1) is 4. The list now has three nodes, so
index 3 holds nothing and get(3) is -1.
Example 2
Input: ops = ["get",
"addAtIndex",
"addAtIndex",
"addAtIndex",
"deleteAtIndex",
"get",
"get"]
args = [0, 1, 5, 0, 5, 1, 8, 2, 0, 1]
[-1,5,8]Explanation: The list starts empty, so get(0) is -1 and addAtIndex(1, 5) does nothing. addAtIndex(0, 5) adds the
first node. addAtIndex(1, 8) uses the list's length as the index, so 8 goes at the end. The list has two
nodes, so deleteAtIndex(2) does nothing.
Example 3
Input: ops = ["addAtHead",
"addAtHead",
"deleteAtIndex",
"get",
"deleteAtIndex",
"get",
"addAtTail",
"get"]
args = [0, 1000000000, 0, 0, 0, 0, 1000000000, 0]
[0,-1,1000000000]Explanation: Each deleteAtIndex(0) removes the current head. After the first one, the head holds 0, which is a real
value, not a miss. After the second, the list is empty, and addAtTail then adds its only node.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ ops.length ≤ 104- Each
ops[i]is"get","addAtHead","addAtTail","addAtIndex"or"deleteAtIndex". argsholds the arguments of the calls in order: one number for each call, and two for eachaddAtIndex.0 ≤ args[i] ≤ 109, so every index and every value is in this range.
Clarifying questions
What you would ask an interviewer before writing any code.