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:


	BrowserHistory(string homepage) Initializes the object with the homepage&nbsp;of the browser.
	void visit(string url)&nbsp;Visits&nbsp;url from the current page. It clears up all the forward history.
	string back(int steps)&nbsp;Move steps back in history. If you can only return x steps in the history and steps &gt; x, you will&nbsp;return only x steps. Return the current url&nbsp;after moving back in history at most steps.
	string forward(int steps)&nbsp;Move steps forward in history. If you can only forward x steps in the history and steps &gt; x, you will&nbsp;forward only&nbsp;x steps. Return the current url&nbsp;after forwarding in history at most steps.


&nbsp;
Example:

Input:
["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
Output:
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]

Explanation:
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
browserHistory.visit("google.com");       // You are in "leetcode.com". Visit "google.com"
browserHistory.visit("facebook.com");     // You are in "google.com". Visit "facebook.com"
browserHistory.visit("youtube.com");      // You are in "facebook.com". Visit "youtube.com"
browserHistory.back(1);                   // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
browserHistory.back(1);                   // You are in "facebook.com", move back to "google.com" return "google.com"
browserHistory.forward(1);                // You are in "google.com", move forward to "facebook.com" return "facebook.com"
browserHistory.visit("linkedin.com");     // You are in "facebook.com". Visit "linkedin.com"
browserHistory.forward(2);                // You are in "linkedin.com", you cannot move forward any steps.
browserHistory.back(2);                   // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
browserHistory.back(7);                   // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"


&nbsp;
Constraints:


	1 &lt;= homepage.length &lt;= 20
	1 &lt;= url.length &lt;= 20
	1 &lt;= steps &lt;= 100
	homepage and url consist of&nbsp; '.' or lower case English letters.
	At most 5000&nbsp;calls will be made to visit, back, and forward.


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

class BrowserHistory:
    def __init__(self, web):
        self.Node = Node(web)
        self.ptr = self.Node

    def visit(self, web):
        self.newWeb = Node(web)
        self.newWeb.prev = self.ptr
        self.ptr.next = self.newWeb
        self.ptr = self.ptr.next

    def back(self, steps):
        i = 0
        while i < steps:
            if self.ptr.prev:
                self.ptr = self.ptr.prev
            else:
                break
            i += 1
        return self.ptr.val

    def forward(self, steps):
        i = 0
        while i < steps:
            if self.ptr.next:
                self.ptr = self.ptr.next
            else:
                break
            i += 1
        return self.ptr.val
class BrowserHistory {
    int current;
    ArrayList<String> history;
    public BrowserHistory(String homepage) {
        this.history = new ArrayList<>();
        history.add(homepage);
        this.current = 0;
    }

    public void visit(String url) {
        while (history.size()-1 > current) {//delete forward history
            history.remove(history.size()-1);//which means delete everything beyond our current website
        }
        history.add(url);
        ++current;
    }

    public String back(int steps) {
        if (steps>current) current = 0;//if we can't get enough back, we return first thing in our history
        else current -= steps;//if there will be no arrayindexoutofrange error, go back
        return history.get(current);//return current webpage
    }

    public String forward(int steps) {
        //if we are going to move more than our arraylist, then we will return the last element
        if (steps+current>=history.size()) current = history.size() - 1;
        else current += steps;//if there will be no arrayindexoutofrange error, go forward!
        return history.get(current);//return the current webpage
    }
}

/**
 * Your BrowserHistory object will be instantiated and called as such:
 * BrowserHistory obj = new BrowserHistory(homepage);
 * obj.visit(url);
 * String param_2 = obj.back(steps);
 * String param_3 = obj.forward(steps);
 */
class Node{
public:
    string url;
    Node* next;
    Node* back;
    Node(string s){
        url = s;
        next = NULL;
        back = NULL;
    }
};

class BrowserHistory {
    Node* head;
public:
    BrowserHistory(string homepage) {
        head = new Node(homepage);
    }

    void visit(string url) {
        Node* ptr = new Node(url);
        head->next = ptr;
        ptr->back = head;
        head = ptr;
    }

    string back(int steps) {
        while(head->back && steps--){ head = head->back; }
        return head->url;
    }

    string forward(int steps) {
        while(head->next && steps--){ head = head->next; }
        return head->url;
    }
};
var BrowserHistory = function(homepage) {
    this.page = {
        url: homepage,
        back: null,
        next: null,
    };
};

BrowserHistory.prototype.visit = function(url) {
    this.page.next = {
        url,
        back: this.page,
        next: null
    };
    this.page = this.page.next;
};

BrowserHistory.prototype.back = function(steps) {
    while (this.page.back && steps) {
        this.page = this.page.back;
        steps--;
    }
    
    return this.page.url;
};

BrowserHistory.prototype.forward = function(steps) {
    while (this.page.next && steps) {
        this.page = this.page.next;
        steps--;
    }
    
    return this.page.url;
};
Design Browser History