function mailDelivery(n, edges):
adj = build adjacency list with edge indices
used = array of false, size = number of edges
// Check degrees
for v from 1 to n:
if adj[v].size is odd:
return "IMPOSSIBLE"
// Hierholzer
stack = [1]
path = []
while stack not empty:
v = stack.top()
while adj[v] not empty and used[adj[v].back().edgeIndex]:
adj[v].pop_back()
if adj[v] not empty:
edge = adj[v].pop_back()
used[edge.index] = true
stack.push(edge.to)
else:
path.append(stack.pop())
return path.reversed()
This runs in time and uses space.