94.20% Faster // All Push & Pop Operations in O(1)
\n\npublic class ListNode\n{\n    public int val;\n    public ListNode next, prv;\n    public ListNode(int val) => this.val = val;\n}\n// All Push and Pop opera
![image](https://assets.leetcode.com/users/images/324ba461-7906-472e-96a3-493cf99402c3_1609441608.7699451.png)\n```\npublic class ListNode\n{\n    public int val;\n    public ListNode next, prv;\n    public ListNode(int val) => this.val = val;\n}\n// All Push and Pop operation in O(1) time\npublic class FrontMiddleBackQueue {\n\n    ListNode front, middle, rear;\n    int count;\n    public FrontMiddleBackQueue()\n    {\n        front = middle = rear = null;\n        count = 0;\n    }\n\n    public void PushFront(int val)\n    {\n        ListNode newNode = new ListNode(val) { next = front, prv = null };\n        if (count == 0)\n            front = middle = rear = newNode;\n        else\n        {\n            front.prv = newNode;\n            front = front.prv;\n            // If list was of odd size update middle => 1 node previous\n            if (count % 2 == 1) middle = middle.prv;\n        }\n        count++;\n    }\n\n    public void PushMiddle(int val)\n    {\n        ListNode newNode = new ListNode(val);\n        if (count == 0)\n            front = middle = rear = newNode;\n        else if (count == 1)\n        {\n            middle.prv = newNode;\n            newNode.next = middle;\n            front = middle = middle.prv;\n        }\n        else\n        {\n            if (count % 2 == 0) // Node Count was Even\n            {\n                newNode.prv = middle;\n                newNode.next = middle.next;\n                middle.next.prv = newNode;\n                middle.next = newNode;\n                middle = middle.next;\n            }\n            else\n            {\n                newNode.next = middle;\n                newNode.prv = middle.prv;\n                middle.prv.next = newNode;\n                middle.prv = newNode;\n                middle = middle.prv;\n            }\n        }\n        count++;\n    }\n\n    public void PushBack(int val)\n    {\n        ListNode newNode = new ListNode(val) { next = null, prv = rear };\n        if (count == 0)\n            front = middle = rear = newNode;\n        else\n        {\n            rear.next = newNode;\n            rear = rear.next;\n            // If list was of even size update middle => 1 node next\n            if (count % 2 == 0) middle = middle.next;\n        }\n        count++;\n    }\n\n    public int PopFront()\n    {\n        if (count == 0) return -1;\n        int ans = front.val;\n\n        if (count == 1)\n            front = middle = rear = null;\n        else\n        {\n            front = front.next;\n            front.prv = null;\n            // If list was of even size update middle => 1 node frwd\n            if (count % 2 == 0) middle = middle.next;\n        }\n        count--;        // decreamenet count\n        return ans;\n    }\n\n    public int PopMiddle()\n    {\n        if (count == 0) return -1;\n        int ans = middle.val;\n\n        if (count == 1)\n            front = middle = rear = null;\n        else \n        {\n            if (middle.prv != null) middle.prv.next = middle.next;\n            if (middle.next != null) middle.next.prv = middle.prv;\n            // If list has just 2 node i.e middle == front than update front 1 position frwd\n            if (count == 2) front = front.next;\n            // If list was of even size update middle => 1 node frwd else 1 node previous\n            middle = (count % 2 == 0) ? middle.next : middle.prv;\n        }\n        count--;        // decreamenet count\n        return ans;\n    }\n\n    public int PopBack()\n    {\n        if (count == 0) return -1;\n        int ans = rear.val;\n\n        if (count == 1)\n            front = middle = rear = null;\n        else\n        {\n            rear.prv.next = rear.next;\n            rear = rear.prv;\n            // If list was of odd size update middle => 1 node previous\n            if (count % 2 == 1) middle = middle.prv;\n        }\n        count--;        // decreamenet count\n        return ans;\n    }\n}\n\n/**\n * Your FrontMiddleBackQueue object will be instantiated and called as such:\n * FrontMiddleBackQueue obj = new FrontMiddleBackQueue();\n * obj.PushFront(val);\n * obj.PushMiddle(val);\n * obj.PushBack(val);\n * int param_4 = obj.PopFront();\n * int param_5 = obj.PopMiddle();\n * int param_6 = obj.PopBack();\n */\n```
[]