- Problem
1472. Design Browser History
- 내 풀이 (Doubly Linked List)
class Node:
def __init__(self, url: str):
self.page: str = url
self.next: Node = None
self.prev: Node = None
class BrowserHistory:
def __init__(self, homepage: str):
self.root = Node(homepage)
def visit(self, url: str) -> None:
self.root.next = Node(url)
self.root.next.prev = self.root
self.root = self.root.next
def back(self, steps: int) -> str:
for _ in range(steps):
if not self.root.prev:
break
self.root = self.root.prev
return self.root.page
def forward(self, steps: int) -> str:
for _ in range(steps):
if not self.root.next:
break
self.root = self.root.next
return self.root.page
- 결과
- 시간 복잡도:
O(N)
- 공간 복잡도:
O(N)