598.Implement a Queue Using a Linked List
Easy
Implement a queue with a linked list and run a list of operations on it.
ops[i] is one of "enqueue" (add args[i] at the back), "dequeue" (remove and report the front item, or −1 if empty), "front" (report the front item, or −1), "isEmpty" (1 or 0) and "size". Return everything reported, in order.
A linked listLinked ListA chain of nodes where each node stores a value and a pointer to the next node. grows one node at a time, so the queue never fills up and nothing is ever shifted. The front is easy to reach through head; the back is the interesting part. Keeping a tail pointerTail PointerA pointer to the last node of a linked list, allowing O(1) insertion at the end. alongside head turns enqueue from an O(n) walk into an O(1) link.
Example 1:
Input: ops = ["enqueue","enqueue","dequeue","front","size"], args = [5,8,0,0,0]
Output: [5,8,1]
Example 2:
Input: ops = ["front","enqueue","front"], args = [0,3,0]
Output: [-1,3]
+ 3 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ ops.length ≤ 1000 - ●
ops[i] is one of "enqueue", "dequeue", "front", "isEmpty", "size" - ●
args[i] is the value for "enqueue" and 0 otherwise
ops =
["enqueue", "enqueue", "dequeue", "front", "size"]
args =
[5, 8, 0, 0, 0]