Skip to content

Instantly share code, notes, and snippets.

@mauricioabreu
Created June 9, 2024 21:32
Show Gist options
  • Save mauricioabreu/90d2e0f3177639842a3f7899a9c5465c to your computer and use it in GitHub Desktop.
Save mauricioabreu/90d2e0f3177639842a3f7899a9c5465c to your computer and use it in GitHub Desktop.
Browser History, leetcode solution
class History:
def __init__(self, url, prev=None, next_node=None):
self.url = url
self.prev = prev
self.next = next_node
class BrowserHistory:
def __init__(self, homepage: str):
self.head = History(homepage)
# time O(1)
# space O(1)
def visit(self, url: str) -> None:
new_page = History(url)
new_page.prev = self.head
self.head.next = new_page
self.head = self.head.next
# time O(n)
# space O(1)
def back(self, steps: int) -> str:
while steps and self.head.prev:
self.head = self.head.prev
steps -= 1
return self.head.url
# time O(n)
# space O(1)
def forward(self, steps: int) -> str:
while steps and self.head.next:
self.head = self.head.next
steps -= 1
return self.head.url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment