Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-012

Doubly Linked List Insert and Delete

Problem

Simulate a doubly linked list with insert and delete operations.

operations contains "insert_head", "insert_tail", "delete", or "to_list"; values provides the value for inserts and for delete (the first occurrence is removed). Record None for every operation except to_list, which records a snapshot of the current list.

Return the list of recorded results.

Hint

Insert at the head with insert(0, value) and store snapshots with list(items).

Test cases

case 1
in: [["insert_tail","insert_tail","insert_head","to_list","delete","to_list"],[1,2,9,0,2,0]]
expected: [null,null,null,[9,1,2],null,[9,1]]
case 2
in: [["delete","to_list"],[5,0]]
expected: [null,[]]
case 3
in: [["insert_head","insert_head","to_list"],[1,2,0]]
expected: [null,null,[2,1]]
case 4
in: [["insert_tail","delete","to_list"],[4,4,0]]
expected: [null,null,[]]
Starter codePython
def doubly_ll_ops(operations, values):
    # Your code here
    pass

4 test cases · run your code against them in the editor