Skip to content

Commit 3458367

Browse files
authored
Merge pull request neetcode-gh#2420 from lokeshmvs21/main
create: 1472-design-browser-history.java
2 parents df51f66 + 12909b0 commit 3458367

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class ListNode{
2+
ListNode back;
3+
String url;
4+
ListNode forward;
5+
public ListNode(String url){
6+
this.url = url;
7+
back = forward = null;
8+
}
9+
}
10+
11+
class BrowserHistory {
12+
13+
ListNode head;
14+
ListNode current;
15+
16+
public BrowserHistory(String homepage) {
17+
head = new ListNode(homepage);
18+
current = head;
19+
}
20+
21+
public void visit(String url) {
22+
current.forward = new ListNode(url);
23+
current.forward.back = current;
24+
current = current.forward;
25+
}
26+
27+
public String back(int steps) {
28+
while(steps>0 && current.back!=null){
29+
current = current.back;
30+
steps--;
31+
}
32+
return current.url;
33+
}
34+
35+
public String forward(int steps) {
36+
while(steps>0 && current.forward!=null){
37+
current = current.forward;
38+
steps--;
39+
}
40+
return current.url;
41+
}
42+
}

0 commit comments

Comments
 (0)