The problem. Build a queue on a singly linked list of ListNodes (each has a val and a next). Support enqueue(x), dequeue() (remove and return the front item, or -1 if empty), front() (the front item, or -1 if empty), isEmpty() and size(). A linked list never runs out of slots, so there is no capacity to manage.
enqueue(10) enqueue(20) front() -> 10
dequeue() -> 10 size() -> 1
dequeue() -> 20 dequeue() -> -1 (empty)A linked list can only cheaply change things it holds a pointer to. So the questions are: which end is the front, which is the back, and which pointers do we keep?
Free account
Sign up to read the rest of this lesson: 6 more sections, 5 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come