코드를 입력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(val=homepage)
def visit(self, url):
self.current.next = ListNode(val=url, prev=self.current)
self.current = self.current.next
return None
def back(self, steps):
while steps > 0 and self.current.prev != None:
steps -= 1
self.current = self.current.prev
return self.current.val
def forwoard(self, steps):
while steps > 0 and self.current.next != None:
steps -= 1
self.current = self.current.next
return self.current.val
browserHistory = BrowserHistory("leetcode.com")
browserHistory.visit("google.com")
browserHistory.visit("facebook.com")
browserHistory.visit("youtube.com")
print(browserHistory.back(1))
print(browserHistory.back(1))
print(browserHistory.forwoard(1))
browserHistory.visit("linkedin.com")
print(browserHistory.forwoard(2))
print(browserHistory.back(2))
print(browserHistory.back(7))하세요
ArrayList
장점 : forward ,back 사용시 index를 이용해서 O(1)에 이용할 수 있음
단점 : visit할때 LinkedList는 연결을 끊어주면 되지만 ArrayList는 하나한 지워줘야한다