https://leetcode.com/problems/design-browser-history/
Design Browser History - LeetCode
Design Browser History - You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: * BrowserHisto
leetcode.com
파이썬 코드
더보기
class ListNode(object):
def __init__(self, val=0, next=None, prev=None):
self.val = val
self.next = next
self.prev = prev
class BrowserHistory(object):
def __init__(self, homepage):
self.head = self.current = ListNode(homepage)
def visit(self, url):
self.current.next = ListNode(val=url, prev=self.current)
self.current = self.current.next
return None
def forward(self, steps):
while steps > 0 and self.current.next is not None:
self.current = self.current.next
steps -= 1
return self.current.val
def back(self, steps):
while steps > 0 and self.current.prev is not None:
self.current = self.current.prev
steps -= 1
return self.current.val