🍵 Simple Approach || 🏃‍♂️ O(n) complexity || 🧑‍💻 4 Different Approach
Intuition\nThe problem seems to require maintaining a queue where elements can be inserted and removed from the front, middle, and back efficiently.\n\n# Approa
# Intuition\nThe problem seems to require maintaining a queue where elements can be inserted and removed from the front, middle, and back efficiently.\n\n# Approach\nI\'ve implemented a custom queue using a linked list. The `pushFront`, `pushMiddle`, and `pushBack` methods insert elements at the front, middle, and back respectively. Similarly, the `popFront`, `popMiddle`, and `popBack` methods remove elements from the front, middle, and back respectively.\n\n# Complexity\n##### This time is only for first Approach\n- Time complexity:\n  - `pushFront`: $$O(1)$$\n  - `pushMiddle`: $$O(n)$$\n  - `pushBack`: $$O(n)$$\n  - `popFront`: $$O(1)$$\n  - `popMiddle`: $$O(n)$$\n  - `popBack`: $$O(n)$$\n- Space complexity:\n  - $$O(n)$$\n\n# Code\n```java []\n// Solve using Singly LinkedList\n// The approach is intricate yet beneficial, especially for those with a curious disposition.\nclass Node {\n    public int val;\n    public Node next;\n    public Node(int val){\n        this.val=val;\n        this.next=null;\n    }\n}\nclass FrontMiddleBackQueue {\n    Node head;\n    int size;\n    public FrontMiddleBackQueue() {\n        head = null;\n        size = 0;\n    }\n    \n    public void pushFront(int val) {\n        //System.out.println("Push Front");\n        Node newNode = new Node(val);\n        if(head == null){\n            head = newNode;\n        }\n        else{\n            newNode.next = head;\n            head = newNode;\n        }\n        size++;\n        //System.out.println(size);\n        //show();\n    }\n    \n    public void pushMiddle(int val) {\n        //System.out.println("Push Middle");\n        Node newNode = new Node(val);\n        if(head == null){\n            head = newNode;\n        }\n        else if(head.next == null){\n            newNode.next = head;\n            head = newNode;\n        }\n        else{\n            int count = 0;\n            Node temp = head;\n            while(temp != null){\n                count++;\n                if(count == size/2){\n                    Node nextNode = temp.next;\n                    temp.next = newNode;\n                    newNode.next = nextNode;\n                    break;\n                }\n                temp = temp.next;\n            }\n        }\n        size++;\n        //System.out.println(size);\n        //show();\n    }\n    \n    public void pushBack(int val) {\n        //System.out.println("Push Back");\n        Node newNode = new Node(val);\n        if(head == null){\n            head = newNode;\n        }\n        else{\n            Node temp = head;\n            while(temp.next != null){\n                temp = temp.next;\n            }\n            temp.next = newNode;\n        }\n        size++;\n        //System.out.println(size);\n        //show();\n    }\n    \n    public int popFront() {\n        //System.out.println("Pop Front");\n\n        if(head == null){\n            return -1;\n        }\n        if(head.next == null){\n            int val = head.val;\n            head = null;\n            size--;\n            //System.out.println(size);\n            //show();\n            return val;\n        }\n        int val = head.val;\n        head = head.next;\n        //show();\n        size--;\n        //System.out.println(size);\n        return val;\n    }\n    \n    public int popMiddle() {\n        //System.out.println("Pop Middle");\n        if(head == null){\n            return -1;\n        }\n        if(head.next == null){\n            int val = head.val;\n            head = null;\n            size--;\n            //System.out.println(size);\n            //show();\n            return val; \n        }\n        if(size == 2){\n            int val = head.val;\n            head = head.next;\n            size--;\n            //System.out.println(size);\n            //show();\n            return val;\n        }\n        int count = 0;\n        Node temp = head;\n        Node prev = null;\n        while(temp != null){\n            if(count == (size - 1)/2){\n                int val = temp.val;\n                prev.next = temp.next;\n                temp = null;\n                size--;\n                //System.out.println(size);\n                //show();\n                return val;\n            }\n            count++;\n            prev = temp;\n            //prev = 4 ,\n            temp = temp.next;\n            // temp = 3\n        }\n        size--;\n        //System.out.println(size);\n        //show();\n        return -1;\n    }\n    \n    public int popBack() {\n        //System.out.println("Pop Back");\n\n        if(head == null){\n            return -1;\n        }\n        if(head.next == null){\n            int val = head.val;\n            head = null;\n            size--;\n            //System.out.println(size);\n            //show();\n            return val;\n        }\n        Node prev = null;\n        Node temp = head;\n        while(temp.next != null){\n            prev = temp;\n            temp = temp.next;\n        }\n        prev.next = null;\n        size--;\n        //System.out.println(size);\n        //show();\n        return temp.val;\n    }\n    // void show(){\n    //     Node temp = head;\n    //     while(temp != null){\n    //         System.out.print(temp.val + "->");\n    //         temp = temp.next;\n    //     }\n    //     //System.out.println("null");\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```\n\n```Java []\n//Solved using Deque\nclass FrontMiddleBackQueue {\n    Deque<Integer> dq1, dq2;\n    public FrontMiddleBackQueue() {        \n        dq1 = new ArrayDeque<Integer>();\n        dq2 = new ArrayDeque<Integer>();\n    }\n    \n    public void pushFront(int val) {\n        dq1.addFirst(val);\n    }    \n    \n    public void pushBack(int val) {\n        dq2.addLast(val);\n    }   \n    \n    public void pushMiddle(int val) {\n        while(dq1.size() + 1 < dq2.size())\n            dq1.addLast(dq2.removeFirst());       \n        while(dq1.size() > dq2.size())\n            dq2.addFirst(dq1.removeLast());        \n        dq1.addLast(val);        \n    }     \n    \n    public int popFront() {\n        if(!dq1.isEmpty())\n            return dq1.removeFirst();\n        if(!dq2.isEmpty())\n            return dq2.removeFirst();\n        return -1;    \n    }  \n    \n    public int popMiddle() {\n        if(dq1.isEmpty() && dq2.isEmpty())\n            return -1;        \n        while(dq1.size() < dq2.size())\n            dq1.addLast(dq2.removeFirst());     \n        while(dq1.size() > dq2.size() + 1)\n            dq2.addFirst(dq1.removeLast());\n        return !dq1.isEmpty() ? dq1.removeLast() : dq2.removeFirst();\n    }    \n    \n    public int popBack() {\n        if(!dq2.isEmpty())\n            return dq2.removeLast();\n        if(!dq1.isEmpty())\n            return dq1.removeLast();\n        return -1;            \n    }    \n}\n```\n```Java []\n// Solved using DoublyLinkedList\nclass FrontMiddleBackQueue {\n    class ListNode{\n        int val;\n        ListNode next;\n        ListNode prev;\n        ListNode(int val){\n            this.val = val;\n        }\n    }\n    \n    int count;\n    ListNode start;\n    ListNode middle;\n    ListNode last;\n    \n    public FrontMiddleBackQueue() {\n        count = 0;\n        //place holders for start, middle, last\n        start = new ListNode(-1);\n        middle = new ListNode(-1);\n        last = new ListNode(-1);\n        \n        start.next = last;\n        last.prev = start;\n    }\n    \n    public void pushFront(int val) {\n        ListNode newNode = new ListNode(val);\n        \n        //insert new node in between start and start.next\n        newNode.next = start.next;\n        newNode.prev = start;\n        \n        //fix start and start.next to connect the new node\n        start.next.prev = newNode;\n        start.next = newNode;\n        \n        //increment count\n        count++;\n        if (count == 1)\n            middle = newNode;   // for first time the newNode will be the middle node\n        else if(count % 2 == 0) // if even store to the left side of existing middle node\n            middle = middle.prev;\n    }\n    \n    public void pushMiddle(int val) {\n        ListNode newNode = new ListNode(val);\n        \n        //if there is not extra node other than start and last then add the node to the middle\n        if (count == 0){\n            newNode.prev = start;\n            newNode.next = last;\n            \n            last.prev = newNode;\n            start.next = newNode;\n        }\n        else if(count % 2 == 0){   // if count is even then newNode will be pushed after middle\n            newNode.next = middle.next;\n            newNode.prev = middle;\n            \n            middle.next.prev = newNode;\n            middle.next = newNode;\n        }\n        else{\n            newNode.prev = middle.prev; // if count is odd then newNode will be pushed before middle\n            newNode.next = middle;\n            \n            middle.prev.next = newNode;\n            middle.prev = newNode;\n        }\n        middle = newNode;\n        count++;\n        return;\n    }\n    \n    public void pushBack(int val) {\n        ListNode newNode = new ListNode(val);\n        \n        // newNode will be inserted before the last\n        newNode.prev = last.prev;\n        newNode.next = last;\n        \n        last.prev.next = newNode;\n        last.prev = newNode;\n        \n        count++;\n        if (count == 1)\n            middle = newNode;\n        else if(count % 2 != 0) // if count is odd then middle will be set to the right side of new node\n            middle = middle.next;\n    }\n    \n    public int popFront() {\n        // no element to pop\n        if (count == 0)\n            return -1;\n        \n        int result = start.next.val;\n        //remove the start.next for pop operation\n        removeNode(start.next);\n        count--;\n        if (count % 2 != 0)\n            middle = middle.next;\n        \n        return result;\n    }\n    \n    public int popMiddle() {\n        // do only if there are elements\n        if (count == 0)\n            return -1;\n        \n        int result = middle.val;\n        removeNode(middle);\n        if (count % 2 != 0) // if count is odd then set middl to the left node\n            middle = middle.prev;\n        else\n            middle = middle.next; // if count is even then set middle to the right node\n        count--;\n        return result;\n    }\n    \n    public int popBack() {\n        // do only if there are elements\n        if (count == 0)\n            return -1;\n        \n        int result = last.prev.val;\n        //remove the last.prev node\n        removeNode(last.prev);\n        count--;\n        if (count % 2 == 0) // if count is even then set middle to the left node\n            middle = middle.prev;\n        return result;\n    }\n    \n    private void removeNode(ListNode node){\n        node.next.prev = node.prev;\n        node.prev.next = node.next;\n    }\n}\n```\n``` Java  []\n// Solved using LinkedList Collection Frameword\n// * Not fit for Interviews)\nclass FrontMiddleBackQueue {\n    LinkedList<Integer> list;\n    \n    public FrontMiddleBackQueue() {\n        list = new LinkedList();\n    }\n    \n    public void pushFront(int val) {\n        list.add(0, val);\n    }\n    \n    public void pushMiddle(int val) {\n        list.add(list.size() / 2, val);\n    }\n    \n    public void pushBack(int val) {\n        list.add(val);\n    }\n    \n    public int popFront() {\n        if(isEmpty()) return -1;\n       \n        return list.remove(0);\n    }\n    \n    public int popMiddle() {\n        if(isEmpty()) return -1;\n        \n        return list.remove(findMiddleIndex());\n    }\n    \n    public int popBack() {\n        if(isEmpty()) return -1;\n        \n        return list.remove(list.size() - 1);\n    }\n    \n    private boolean isEmpty(){\n        return list.isEmpty();\n    }\n    \n    private int findMiddleIndex(){\n        return (list.size() - 1) / 2;\n    }\n    \n\n}\n```\n\n\n`Please Upvote \uD83D\uDC4D`\n\n
['Linked List', 'Design', 'Queue', 'Doubly-Linked List', 'Java']