---
# System prepended metadata

title: 0379. Design Phone Directory
tags: [Leetcode, Linked List, Design, Medium]

---

# 0379. Design Phone Directory
###### tags: `Leetcode` `Medium` `Design` `Linked List`
Link: https://leetcode.com/problems/design-phone-directory/description/
## 思路
LinkedList的思想
用一个linkedlist记录所有空的node 然后维护一个pointer指向linkedlist的头
如果linkedlist的头是available的 get()就return它 然后我们把linkedlist的头挪到下一个available node
对于release() 如果这个node本来就是空的我们就什么都不做 如果不是我们把它接在linkedlist的前面 并把linkedlist的头换成这个node
## Code
```java=
class PhoneDirectory {
    int[] next;
    int curr;
    public PhoneDirectory(int maxNumbers) {
        next = new int[maxNumbers];
        for(int i=0; i<maxNumbers; i++){
            next[i] = (i+1)%maxNumbers;
        }
        curr = 0;
    }
    
    public int get() {
        if(next[curr]==-1) return -1;
        int ans = curr;
        curr = next[curr];
        next[ans]=-1;
        return ans;
    }
    
    public boolean check(int number) {
        return next[number]!=-1;
    }
    
    public void release(int number) {
        if(next[number]!=-1) return;
        next[number] = curr;
        curr = number;
    }
}
```