Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-122

Deque via Doubly Linked List

Problem

Simulate a deque backed by a doubly linked list.

operations contains "push_front", "push_back", "pop_front", "pop_back", "remove" (first occurrence of values[i], records whether it existed), "reverse", or "to_list" (snapshot). Pushes and reverse record None and pops return the removed value or None when empty.

Return the list of recorded results.

Hint

A doubly linked list allows O(1) pushes and pops at both ends, while remove scans for the value.

Test cases

case 1
in: [["push_back","push_back","push_front","to_list","reverse","to_list","remove","to_list"],[2,3,1,0,0,0,3,0]]
expected: [null,null,null,[1,2,3],null,[3,2,1],true,[2,1]]
case 2
in: [["pop_front","pop_back"],[0,0]]
expected: [null,null]
case 3
in: [["push_front","to_list"],[7,0]]
expected: [null,[7]]
case 4
in: [["remove","push_back","remove","to_list"],[5,5,5,0]]
expected: [false,null,true,[]]
Starter codePython
def dll_deque_ops(operations, values):
    # Your code here
    pass

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