Problem statement

https://binarysearch.com/problems/Reverse-Graph/

Solution

Just do what is asked: traverse edges and add opposite edge in G2.

Complexity

It is O(E) for time and space.

Code

class Solution:
    def solve(self, G):
        n = len(G)
        G2 = [[] for _ in range(n)]
        for i in range(n):
            for j in G[i]:
                G2[j] += [i]

        return [sorted(G2[i]) for i in range(n)]