| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.DesignBrowserHistory; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class DesignBrowserHistoryLinkedList : IDesignBrowserHistory |
| | | 16 | | { |
| | | 17 | | private Node _current; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Time complexity - O(1) |
| | | 21 | | /// Space complexity - O(1) |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="homepage"></param> |
| | 1 | 24 | | public DesignBrowserHistoryLinkedList(string homepage) |
| | 1 | 25 | | { |
| | 1 | 26 | | _current = new Node(homepage); |
| | 1 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Time complexity - O(1) |
| | | 31 | | /// Space complexity - O(1) |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="url"></param> |
| | | 34 | | public void Visit(string url) |
| | 4 | 35 | | { |
| | 4 | 36 | | _current.Next = new Node(url) |
| | 4 | 37 | | { |
| | 4 | 38 | | Previous = _current |
| | 4 | 39 | | }; |
| | | 40 | | |
| | 4 | 41 | | _current = _current.Next; |
| | 4 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Time complexity - O(n), where n is the number of steps |
| | | 46 | | /// Space complexity - O(1) |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="steps"></param> |
| | | 49 | | /// <returns></returns> |
| | | 50 | | public string Back(int steps) |
| | 4 | 51 | | { |
| | 18 | 52 | | for (var i = 0; i < steps; i++) |
| | 6 | 53 | | { |
| | 6 | 54 | | if (_current.Previous == null) |
| | 1 | 55 | | { |
| | 1 | 56 | | break; |
| | | 57 | | } |
| | | 58 | | |
| | 5 | 59 | | _current = _current.Previous; |
| | 5 | 60 | | } |
| | | 61 | | |
| | 4 | 62 | | return _current.Value; |
| | 4 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Time complexity - O(n), where n is the number of steps |
| | | 67 | | /// Space complexity - O(1) |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="steps"></param> |
| | | 70 | | /// <returns></returns> |
| | | 71 | | public string Forward(int steps) |
| | 2 | 72 | | { |
| | 6 | 73 | | for (var i = 0; i < steps; i++) |
| | 2 | 74 | | { |
| | 2 | 75 | | if (_current.Next == null) |
| | 1 | 76 | | { |
| | 1 | 77 | | break; |
| | | 78 | | } |
| | | 79 | | |
| | 1 | 80 | | _current = _current.Next; |
| | 1 | 81 | | } |
| | | 82 | | |
| | 2 | 83 | | return _current.Value; |
| | 2 | 84 | | } |
| | | 85 | | |
| | | 86 | | private sealed class Node |
| | | 87 | | { |
| | 5 | 88 | | public Node(string value) |
| | 5 | 89 | | { |
| | 5 | 90 | | Value = value; |
| | 5 | 91 | | } |
| | | 92 | | |
| | 6 | 93 | | public string Value { get; } |
| | 15 | 94 | | public Node? Previous { get; set; } |
| | 11 | 95 | | public Node? Next { get; set; } |
| | | 96 | | } |
| | | 97 | | } |