Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create 1472-Design-Browser-History.py
  • Loading branch information
hrishikeshtak committed Oct 6, 2022
commit ea95ee9272f40dec2acfae6041f843d864722011
57 changes: 57 additions & 0 deletions python/1472-Design-Browser-History.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
1472. Design Browser History
"""


class DLLNode:
def __init__(self, val):
self.val = val
self.prev = None
self.next = None


class BrowserHistory:
def __init__(self, homepage: str):
# initialize head and tail to dummy node
self.head = DLLNode(-1)
self.tail = DLLNode(-1)

self.head.next = self.tail
self.tail.prev = self.head

# insert homepage at head node
self.insertAtHead(homepage)
# update cur pointer to homepage
self.cur = self.head.next

def insertAtHead(self, homepage: str):
temp = DLLNode(homepage)
temp.next = self.head.next
temp.prev = self.head

self.tail.prev = temp
self.head.next = temp

def visit(self, url: str) -> None:
temp = DLLNode(url)
# clears forward history
temp.next = self.tail
temp.prev = self.cur
self.tail.prev = temp
self.cur.next = temp
# update cur pointer to URL
self.cur = self.cur.next

def back(self, steps: int) -> str:
i = 0
while self.cur.prev != self.head and i < steps:
self.cur = self.cur.prev
i += 1
return self.cur.val

def forward(self, steps: int) -> str:
i = 0
while self.cur.next != self.tail and i < steps:
self.cur = self.cur.next
i += 1
return self.cur.val