https://leetcode.com/problems/design-browser-history/
파이썬 코드
더보기
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