leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1472.cpp (882B)


      1 class BrowserHistory {
      2     struct Node {
      3         Node *next, *prev;
      4         string val;
      5         Node(string val = "#", Node *prev = nullptr, Node *next = nullptr)
      6             : val(val), prev(prev), next(next) {}
      7     };
      8     Node *head = nullptr, *tail = nullptr, *crnt = nullptr;
      9 
     10   public:
     11     BrowserHistory(string homepage) { crnt = head = tail = new Node(homepage); }
     12 
     13     void visit(string url) {
     14         for (Node *t = tail->next; t;) {
     15             Node *tmp = t;
     16             t = t->next;
     17             delete tmp;
     18         }
     19         crnt = tail = tail->next = new Node(url, tail, nullptr);
     20     }
     21 
     22     string back(int steps) {
     23         while (steps-- && crnt->prev)
     24             tail = crnt = crnt->prev;
     25         return crnt->val;
     26     }
     27 
     28     string forward(int steps) {
     29         while (steps-- && crnt->next)
     30             tail = crnt = crnt->next;
     31         return crnt->val;
     32     }
     33 };