| 9 | } |
| 10 | |
| 11 | class LinkedList { |
| 12 | constructor() { |
| 13 | this.map = new Map(); |
| 14 | this.Right = new ListNode(null); |
| 15 | this.Left = new ListNode(null, this.Right); |
| 16 | this.Right.prev = this.Left; |
| 17 | } |
| 18 | |
| 19 | len() { |
| 20 | return this.map.size; |
| 21 | } |
| 22 | |
| 23 | pop(val) { |
| 24 | if (this.map.has(val)) { |
| 25 | let node = this.map.get(val); |
| 26 | |
| 27 | // save |
| 28 | let prevNode = node.prev; |
| 29 | // delete |
| 30 | prevNode.next = node.next; |
| 31 | prevNode.next.prev = prevNode; |
| 32 | this.map.delete(val); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | popleft() { |
| 37 | let data = this.Left.next.val; |
| 38 | this.Left.next = this.Left.next.next; |
| 39 | this.Left.next.prev = this.Left; |
| 40 | this.map.delete(data); |
| 41 | return data; |
| 42 | } |
| 43 | |
| 44 | pushRight(val) { |
| 45 | let newNode = new ListNode(val, this.Right); |
| 46 | newNode.prev = this.Right.prev; |
| 47 | newNode.prev.next = newNode; |
| 48 | this.Right.prev = newNode; |
| 49 | this.map.set(val, newNode); |
| 50 | } |
| 51 | |
| 52 | update(val) { |
| 53 | this.pop(val); |
| 54 | this.pushRight(val); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param {number} capacity |
nothing calls this directly
no outgoing calls
no test coverage detected