Implement a class with the following methods:
push_first(x)
: add the numberx
to the beginning of the listpush_last(x)
: add the numberx
to the end of the listpop_first()
: return and remove the element at the beginning of the listpop_last()
: return and remove the element at the end of the listflip()
: reverse the contents of the list
Each method should work in O(1) time.
In a file fliplist.py
, implement a class FlipList
according to the following template.
class FlipList: def __init__(self): # TODO def push_first(self, x): # TODO def push_last(self, x): # TODO def pop_first(self): # TODO def pop_last(self): # TODO def flip(self): # TODO if __name__ == "__main__": f = FlipList() f.push_last(1) f.push_last(2) f.push_last(3) print(f.pop_first()) # 1 f.flip() print(f.pop_first()) # 3