content: class Solution {
      public List<Integer> eventualSafeNodes(int[][] graph) {
          int n = graph.length;
          int[] indegree = new int[n];
          List<List<Integer>> adj = new ArrayList<>();
  
          for(int i = 0; i < n; i++) {
              adj.add(new ArrayList<>());
          }
  
          for (int i = 0; i < n; i++) {
              for (int node : graph[i]) {
                  adj.get(node).add(i);
                  indegree[i]++;
              }
          }
  
          Queue<Integer> q = new LinkedList<>();
          // Push all the nodes with indegree zero in the queue.
          for (int i = 0; i < n; i++) {
              if (indegree[i] == 0) {
                  q.add(i);
              }
          }
  
          boolean[] safe = new boolean[n];
          while (!q.isEmpty()) {
              int node = q.poll();
              safe[node] = true;
  
              for (int neighbor : adj.get(node)) {
                  // Delete the edge "node -> neighbor".
                  indegree[neighbor]--;
                  if (indegree[neighbor] == 0) {
                      q.add(neighbor);
                  }
              }
          }
  
          List<Integer> safeNodes = new ArrayList<>();
          for (int i = 0; i < n; i++) {
              if (safe[i]) {
                  safeNodes.add(i);
              }
          }
          return safeNodes;
      }
  }