Clean Codes🔥🔥|| Full Explanation✅|| Doubly Linked List✅|| C++|| Java|| Python3
What Problem says ?\n- Create a class called BrowserHistory that keeps track of the web pages a user has visited in a web browser. \n- Also make a visit method 
# What Problem says ?\n- Create a class called `BrowserHistory` that keeps track of the web pages a user has visited in a web browser. \n- Also make a `visit` method to add new pages to the `history` and `back` and `forward` methods to move the user back and forward in their browsing history. \n\n# Approach : Using Doubly Linked List\n- Define a `Node` class which represents a node in a linked list. Each node contains a URL and has references to the previous and next nodes in the list.\n- In `BrowserHistory` class ,use the Node class to implement a browser history that allows the user to navigate back and forward through visited URLs. \n- It has a constructor that takes a homepage URL, and a visit method that adds a new URL to the history by creating a new node and updating the current node\'s next reference to point to it.\n- In `back` method ,navigate backwards through the history by updating the current node\'s reference to the previous node, until the desired number of steps have been taken or there are no more previous nodes. and return the URL of the current node.\n- In `forward` method ,navigate forward through the history by updating the current node\'s reference to the next node, until the desired number of steps have been taken or there are no more next nodes and return the URL of the current node.\n<!-- Describe your approach to solving the problem. -->\n\n# Let\'s See an Example :\n```\nBrowserHistory history = new BrowserHistory("https://www.google.com");\n\n// Visit some websites\nhistory.visit("https://en.wikipedia.org");\nhistory.visit("https://www.nytimes.com");\nhistory.visit("https://www.youtube.com");\n\n// Go back to the previous website twice\nSystem.out.println(history.back(2)); // Outputs: https://en.wikipedia.org\n\n// Go forward to the next website\nSystem.out.println(history.forward(1)); // Outputs: https://www.nytimes.com\n\n// Visit a new website\nhistory.visit("https://www.reddit.com");\n\n// Go back to the previous website\nSystem.out.println(history.back(1)); // Outputs: https://www.nytimes.com\n\n\n```\n- In this example, we create a new `BrowserHistory` object with the homepage set to `https://www.google.com.` We then visit some websites and move back and forward through the history using the `back` and `forward` methods. Finally, we visit a new website and go back to the previous site again.\n# Complexity :\n- Time complexity : O(steps)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution.\uD83D\uDE0A\n```\n# Codes [C++ |Java |Python3] : With Comments\n```Java []\n// A class to represent a node in a linked list\nclass Node {\n  // The previous and next nodes in the list\n  public Node prev;\n  public Node next;\n  // The URL represented by this node\n  public final String url;\n  \n  // Constructor that sets the URL for this node\n  public Node(final String url) {\n    this.url = url;\n  }\n}\n\n// A class to represent a browser history\nclass BrowserHistory {\n  // The current node in the history\n  private Node curr;\n  \n  // Constructor that creates a new history with the given homepage\n  public BrowserHistory(String homepage) {\n    // Create a new node to represent the homepage\n    curr = new Node(homepage);\n  }\n\n  // Method to add a new URL to the history\n  public void visit(String url) {\n    // Create a new node to represent the new URL\n    curr.next = new Node(url);\n    // Set the previous node for the new node to be the current node\n    curr.next.prev = curr;\n    // Make the new node the current node\n    curr = curr.next;\n  }\n\n  // Method to navigate back in the history by the given number of steps\n  public String back(int steps) {\n    // While there are previous nodes and we haven\'t gone back enough steps yet\n    while (curr.prev != null && steps-- > 0) {\n      // Move back one node by setting the current node to the previous node\n      curr = curr.prev;\n    }\n    // Return the URL represented by the current node\n    return curr.url;\n  }\n\n  // Method to navigate forward in the history by the given number of steps\n  public String forward(int steps) {\n    // While there are next nodes and we haven\'t gone forward enough steps yet\n    while (curr.next != null && steps-- > 0) {\n      // Move forward one node by setting the current node to the next node\n      curr = curr.next;\n    }\n    // Return the URL represented by the current node\n    return curr.url;\n  }\n}\n\n```\n```C++ []\n// A struct to represent a node in a doubly linked list\nstruct Node {\n  // The previous and next nodes in the list\n  Node* prev = nullptr;\n  Node* next = nullptr;\n  // The URL represented by this node\n  const string url;\n\n  // Constructor that sets the URL for this node\n  Node(const string& url) : url(url) {}\n};\n\n// A class to represent a browser history\nclass BrowserHistory {\n public:\n  // Constructor that creates a new history with the given homepage\n  BrowserHistory(string homepage) {\n    // Create a new node to represent the homepage\n    curr = new Node(homepage);\n  }\n\n  // Method to add a new URL to the history\n  void visit(string url) {\n    // Create a new node to represent the new URL\n    curr->next = new Node(url);\n    // Set the previous node for the new node to be the current node\n    curr->next->prev = curr;\n    // Make the new node the current node\n    curr = curr->next;\n  }\n\n  // Method to navigate back in the history by the given number of steps\n  string back(int steps) {\n    // While there are previous nodes and we haven\'t gone back enough steps yet\n    while (curr->prev && steps-- > 0) {\n      // Move back one node by setting the current node to the previous node\n      curr = curr->prev;\n    }\n    // Return the URL represented by the current node\n    return curr->url;\n  }\n\n  // Method to navigate forward in the history by the given number of steps\n  string forward(int steps) {\n    // While there are next nodes and we haven\'t gone forward enough steps yet\n    while (curr->next && steps-- > 0) {\n      // Move forward one node by setting the current node to the next node\n      curr = curr->next;\n    }\n    // Return the URL represented by the current node\n    return curr->url;\n  }\n\n private:\n  // The current node in the history\n  Node* curr = nullptr;\n};\n\n```\n```Python []\n# A class to represent a node in a doubly linked list\nclass Node:\n  def __init__(self, url: str):\n    # The previous and next nodes in the list\n    self.prev = None\n    self.next = None\n    # The URL represented by this node\n    self.url = url\n\n# A class to represent a browser history\nclass BrowserHistory:\n  def __init__(self, homepage: str):\n    # Create a new node to represent the homepage\n    self.curr = Node(homepage)\n\n  # Method to add a new URL to the history\n  def visit(self, url: str) -> None:\n    # Create a new node to represent the new URL\n    self.curr.next = Node(url)\n    # Set the previous node for the new node to be the current node\n    self.curr.next.prev = self.curr\n    # Make the new node the current node\n    self.curr = self.curr.next\n\n  # Method to navigate back in the history by the given number of steps\n  def back(self, steps: int) -> str:\n    # While there are previous nodes and we haven\'t gone back enough steps yet\n    while self.curr.prev and steps > 0:\n      # Move back one node by setting the current node to the previous node\n      self.curr = self.curr.prev\n      steps -= 1\n    # Return the URL represented by the current node\n    return self.curr.url\n\n  # Method to navigate forward in the history by the given number of steps\n  def forward(self, steps: int) -> str:\n    # While there are next nodes and we haven\'t gone forward enough steps yet\n    while self.curr.next and steps > 0:\n      # Move forward one node by setting the current node to the next node\n      self.curr = self.curr.next\n      steps -= 1\n    # Return the URL represented by the current node\n    return self.curr.url\n\n```\n\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\n![ezgif-3-22a360561c.gif](https://assets.leetcode.com/users/images/94c993ff-345a-40f1-a095-170c8692857b_1679107487.1779711.gif)\n
['Doubly-Linked List', 'Python', 'C++', 'Java', 'Python3']