2 Approach| Stack And Linked List | Explained
 !- Approach 1 Using Two Stacks -!\n\n## Intuition\nUse two stacks: one for back history, and one for forward history. You can simulate the functions by popping
<h1> !- Approach 1 Using Two Stacks -!\n\n## Intuition\nUse two stacks: one for back history, and one for forward history. You can simulate the functions by popping an element from one stack and pushing it into the other.\n\n# Approach\nDetailed approach for the provided solution, which simulates a web browser history using two lists: `self.cur` for the current page history and `self.forward_` for the forward history.\n\n1. **Initialization (`__init__` method)**:\n   - The `BrowserHistory` class is initialized with the `homepage`, which is the page where the browsing session starts.\n   - The `self.cur` list is created, initially containing only the `homepage`. This list will be used to represent the current page and the back history.\n   - The `self.forward_` list is created as an empty list to represent the forward history.\n\n2. **Visiting a New URL (`visit` method)**:\n   - When a user visits a new URL, the `visit` method is called with the `url` as a parameter.\n   - The `url` is appended to the `self.cur` list to represent the current page history. This indicates that the user has visited a new page.\n   - To maintain the forward history, the `self.forward_` list is cleared because visiting a new URL invalidates the forward history.\n\n3. **Navigating Backward (`back` method)**:\n   - The `back` method allows the user to go back a certain number of steps in the browsing history.\n   - A loop is used to iterate while there are more than one page in the `self.cur` list (to ensure at least one page remains) and while there are still `steps` left to go back.\n   - In each iteration of the loop, the most recent page (the last URL in `self.cur`) is popped from the `self.cur` list.\n   - The popped URL is then added to the `self.forward_` list to maintain a record of pages that were navigated away from.\n   - The `steps` variable is decremented in each iteration, and this process continues until the desired number of steps have been taken.\n   - After going back, the URL of the current page (which is now the last URL in `self.cur`) is returned.\n\n4. **Navigating Forward (`forward` method)**:\n   - The `forward` method allows the user to go forward a certain number of steps in the browsing history.\n   - It first checks if the `self.forward_` list is empty, which means there is no forward history to navigate.\n   - If the `self.forward_` list is empty, it simply returns the URL of the current page (the last URL in `self.cur`).\n   - If there is a forward history, a loop is used to iterate while there are URLs in the `self.forward_` list and while there are still `steps` left to go forward.\n   - In each iteration, the most recent URL from the forward history (`self.forward_`) is popped and added back to the `self.cur` list to simulate going forward.\n   - The `steps` variable is decremented in each iteration, and this process continues until the desired number of steps have been taken.\n   - After going forward, the URL of the current page (which is now the last URL in `self.cur`) is returned.\n\n# Complexity\n- Time complexity:\n  - `visit` method: O(1)\n  - `back` method: O(steps), where `steps` is the number of steps to go back.\n  - `forward` method: O(steps), where `steps` is the number of steps to go forward.\n\n- Space complexity:\n  - The space complexity is primarily determined by the storage of URLs in the `self.cur` and `self.forward_` lists.\n  - `self.cur` stores the current page history and potentially past pages, so its space complexity is O(n), where n is the number of pages visited.\n  - `self.forward_` stores URLs representing the forward history, and its space complexity is also O(n) in the worst case.\n  - Overall, the space complexity is O(n), where n is the total number of unique pages visited during the browsing session.\n\n\n```c++ []\n class BrowserHistory {\nprivate:\n    vector<string> cur;      // Current page list\n    vector<string> forward_; // Forward history list\n\npublic:\n    BrowserHistory(string homepage) {\n        cur.push_back(homepage);\n        forward_.clear();\n    }\n\n    void visit(string url) {\n        // Visit a new URL, append it to the current page list, and clear the forward history.\n        cur.push_back(url);\n        forward_.clear();\n    }\n\n    string back(int steps) {\n        while (cur.size() > 1 && steps > 0) {\n            // Go back a certain number of steps by popping URLs from the current page list\n            // and adding them to the forward history.\n            string url = cur.back();\n            cur.pop_back();\n            forward_.push_back(url);\n            steps--;\n        }\n\n        // Retrieve the current page URL after going back.\n        return cur.back();\n    }\n\n    string forward(int steps) {\n        if (forward_.empty()) {\n            return cur.back();\n        }\n\n        while (!forward_.empty() && steps > 0) {\n            // Go forward a certain number of steps by popping URLs from the forward history\n            // and adding them back to the current page list.\n            string url = forward_.back();\n            forward_.pop_back();\n            cur.push_back(url);\n            steps--;\n        }\n\n        // Retrieve the current page URL after going forward.\n        return cur.back();\n    }\n};\n\n```\n\n```python []\n class BrowserHistory:\n    def __init__(self, homepage: str):\n        self.cur = [homepage]  # Initialize the current page list with the homepage.\n        self.forward_ = []     # Initialize an empty forward history list.\n\n    def visit(self, url: str) -> None:\n        # Visit a new URL, append it to the current page list, and clear the forward history.\n        self.cur.append(url)\n        self.forward_ = []\n\n    def back(self, steps: int) -> str:\n        while len(self.cur) > 1 and steps:\n            # Go back a certain number of steps by popping URLs from the current page list\n            # and adding them to the forward history.\n            url = self.cur.pop()\n            self.forward_.append(url)\n            steps -= 1\n\n        # Retrieve the current page URL after going back.\n        url = self.cur[-1]\n        return url\n\n    def forward(self, steps: int) -> str:\n        if not self.forward_:\n            return self.cur[-1]\n\n        while self.forward_ and steps:\n            # Go forward a certain number of steps by popping URLs from the forward history\n            # and adding them back to the current page list.\n            url = self.forward_.pop()\n            self.cur.append(url)\n            steps -= 1\n\n        # Retrieve the current page URL after going forward.\n        url = self.cur[-1]\n        return url\n\n```\n \n<h1> !- Approach 2 Using Doubly Linked List -! </h1>\n\n\n```C++ []\nclass Node {\npublic:\n    string url;\n    Node* prev;\n    Node* next;\n\n    Node(string _url) : url(_url), prev(nullptr), next(nullptr) {}\n};\n\nclass BrowserHistory {\nprivate:\n    Node* current;\npublic:\n    BrowserHistory(string homepage) {\n        current = new Node(homepage);\n    }\n\n    void visit(string url) {\n        Node* newNode = new Node(url);\n        newNode->prev = current;\n        current->next = newNode;\n        current = newNode;\n    }\n\n    string back(int steps) {\n        while (current->prev && steps > 0) {\n            current = current->prev;\n            steps--;\n        }\n        return current->url;\n    }\n\n    string forward(int steps) {\n        while (current->next && steps > 0) {\n            current = current->next;\n            steps--;\n        }\n        return current->url;\n    }\n};\n```\n# Please Upvote\n
['Linked List', 'Stack', 'Doubly-Linked List', 'C++', 'Python3']