user
Description

Hierholzer's Algorithm is an efficient way to find Eulerian circuits in a graph.

```
Algorithm: Hierholzer's Algorithm
Input: Graph G = (V, E) represented as adjacency list
Output: Eulerian circuit as ordered list of vertices

Function FindEulerianCircuit(Graph G):
    // Initialize empty circuit and stack
    Circuit = empty list
    Stack = empty stack
    
    // Start from any vertex with non-zero degree
    current = any vertex v in V where degree(v) > 0
    Push current to Stack
    
    While Stack is not empty:
        if OutDegree(current) == 0:
            // Add to circuit when no more unused edges
            Add current to front of Circuit
            current = Pop from Stack
        else:
            // Follow an unused edge
            Push current to Stack
            next = any adjacent vertex with unused edge from current
            Remove edge (current, next) from G
            current = next
            
    Return Circuit

Function VerifyEulerianCircuit(Graph G):
    For each vertex v in G:
        If InDegree(v) ≠ OutDegree(v):
            Return false
    If Graph is not strongly connected:
        Return false
    Return true

Main Algorithm:
    1. If not VerifyEulerianCircuit(G):
        Return "No Eulerian circuit exists"
        
    2. Circuit = FindEulerianCircuit(G)
    
    3. If Circuit contains all edges:
        Return Circuit
    Else:
        Return "No Eulerian circuit exists"

Preconditions:
    - Graph must be connected
    - All vertices must have equal in-degree and out-degree

Time Complexity: O(|E|) where E is the number of edges
Space Complexity: O(|V|) where V is the number of vertices
```

Task

Implement the Hierholze's Algorithm in your language and test it using the same examples as in the OCaml entry.
assistant
```
say join ' -> ', .&hierholser for ([1],[2],[0]), ([1,6],[2],[0,3],[4],[2,5],[0],[4]);

sub hierholser (@graph) {
    my @cycle = 0;
    @cycle.push: @graph[@cycle.tail].pop while @graph[@cycle.tail].elems;
    @cycle; 
}

```

Output:

```
0 -> 1 -> 2 -> 0
0 -> 6 -> 4 -> 5 -> 0 -> 1 -> 2 -> 3 -> 4 -> 2 -> 0
```

You may Attempt This Online!