topological sort with inner class

public class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { // each node is a course // idx is the course label // inDegree means the # of edges point to it // outEdge: point to other courses class Node { int idx; int inDegree; List<Integer> outEdge; Node(int idx) { inDegree = 0; this.idx = idx; outEdge = new ArrayList<>(); } } // init List<Node> graph = new ArrayList<>(); for (int i = 0; i < numCourses; i++) { graph.add(new Node(i)); } // construct the edge for each node for (int i = 0; i < prerequisites.length; i++) { int pre = prerequisites[i][1]; int cur = prerequisites[i][0]; graph.get(pre).outEdge.add(cur); graph.get(cur).inDegree++; } // add no in coming edge node to queue Queue<Node> que = new ArrayDeque<>(); for (int i = 0; i < graph.size(); i++) { if (graph.get(i).inDegree == 0) { que.offer(graph.get(i)); } } int p = 0; int[] res = new int[numCourses]; while (!que.isEmpty()) { Node n = que.poll(); res[p++] = n.idx; for (int i = 0; i < n.outEdge.size(); i++) { Node next = graph.get(n.outEdge.get(i)); next.inDegree--; // when in coming edge number is 0 // add this course into queue if (next.inDegree == 0) { que.offer(next); } } } // have a solution? return p == numCourses? res : new int[0]; } }Course Schedule II