{"id":22513693,"url":"https://github.com/emahtab/lru-cache","last_synced_at":"2026-03-19T23:03:07.919Z","repository":{"id":79525617,"uuid":"240751884","full_name":"eMahtab/lru-cache","owner":"eMahtab","description":"LRU Cache","archived":false,"fork":false,"pushed_at":"2024-10-29T16:36:53.000Z","size":6,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:23:21.314Z","etag":null,"topics":["doubly-linked-list","hashmap","leetcode","lru-cache"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-15T16:49:30.000Z","updated_at":"2024-10-29T16:37:53.000Z","dependencies_parsed_at":"2023-05-10T17:15:27.004Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/lru-cache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flru-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flru-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flru-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flru-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/lru-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950871,"owners_count":20699159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["doubly-linked-list","hashmap","leetcode","lru-cache"],"created_at":"2024-12-07T03:14:04.688Z","updated_at":"2026-01-07T02:36:57.723Z","avatar_url":"https://github.com/eMahtab.png","language":null,"readme":"# LRU Cache\n## https://leetcode.com/problems/lru-cache\n\n\n## Implementation :\n\n```java\nclass LRUCache {\n    Map\u003cInteger, ListNode\u003e hashtable = new HashMap\u003cInteger, ListNode\u003e();\n    ListNode head, tail;\n    int totalItemsInCache, maxCapacity;\n    \n    private class ListNode {\n      int key, value;\n      ListNode next, prev;\n    }\n\n    public LRUCache(int capacity) {\n      this.totalItemsInCache = 0;\n      this.maxCapacity = capacity;\n      head = new ListNode();\n      tail = new ListNode();\n      head.next = tail;\n      tail.prev = head;\n    }\n    \n    public int get(int key) {\n      ListNode node = hashtable.get(key);\n      if (node == null) \n         return -1;\n      moveToHead(node);\n      return node.value;  \n    }\n    \n    private void moveToHead(ListNode node) {\n      removeFromList(node);\n      addToFront(node);\n    }\n    \n    private void removeFromList(ListNode node) {\n      ListNode prevNode = node.prev;\n      ListNode nextNode = node.next;\n      prevNode.next = nextNode;\n      nextNode.prev = prevNode;\n    }\n    \n    private void addToFront(ListNode node) {\n      // Wire up the new node being to be inserted\n      node.prev = head;\n      node.next = head.next;\n      head.next.prev = node;\n      head.next = node;\n    }\n    \n    public void put(int key, int value) {\n      ListNode node = hashtable.get(key);\n      if (node == null) {\n        ListNode newNode = new ListNode();\n        newNode.key = key;\n        newNode.value = value;\n        hashtable.put(key, newNode);\n        addToFront(newNode);\n        totalItemsInCache++;\n        if (totalItemsInCache \u003e maxCapacity) {\n          ListNode tailNode = tail.prev;\n          removeFromList(tailNode);\n          hashtable.remove(tailNode.key);\n          --totalItemsInCache;\n        }\n      } else {\n        node.value = value;\n        moveToHead(node);\n      }\n    }\n}\n\n```\n\n# References :\n1. https://www.youtube.com/watch?v=S6IfqDXWa10\n2. https://github.com/bephrem1/backtobackswe/blob/master/Linked%20Lists/LRUCache/LRUCache.java\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Flru-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Flru-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Flru-cache/lists"}