leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1472.cpp (882B)
0 class BrowserHistory {
1 struct Node {
2 Node *next, *prev;
3 string val;
4 Node(string val = "#", Node *prev = nullptr, Node *next = nullptr)
5 : val(val), prev(prev), next(next) {}
6 };
7 Node *head = nullptr, *tail = nullptr, *crnt = nullptr;
9 public:
10 BrowserHistory(string homepage) { crnt = head = tail = new Node(homepage); }
12 void visit(string url) {
13 for (Node *t = tail->next; t;) {
14 Node *tmp = t;
15 t = t->next;
16 delete tmp;
17 }
18 crnt = tail = tail->next = new Node(url, tail, nullptr);
19 }
21 string back(int steps) {
22 while (steps-- && crnt->prev)
23 tail = crnt = crnt->prev;
24 return crnt->val;
25 }
27 string forward(int steps) {
28 while (steps-- && crnt->next)
29 tail = crnt = crnt->next;
30 return crnt->val;
31 }
32 };