import java.util.ArrayList;
import java.util.HashMap;

public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
        int[] visited = new int[numCourses];
        for (int[] prerequisite : prerequisites) {
            if (!map.containsKey(prerequisite[1])) map.put(prerequisite[1], new ArrayList<>());
            map.get(prerequisite[1]).add(prerequisite[0]);
        }

        for (int i = 0; i < visited.length; i++) {
            if (visited[i] == 0) {
                if (!travel(i, visited, map)) return false;
            }
        }
        return true;
    }

    private boolean travel(int course, int[] visited, HashMap<Integer, ArrayList<Integer>> map) {
        if (visited[course] == -1) return false;
        if (visited[course] == 1) return true;
        visited[course] = -1;
        ArrayList<Integer> tmp = map.get(course);
        if (tmp != null) {
            for (int nextCourse : tmp) {
                if (!travel(nextCourse, visited, map)) return false;
            }
        }
        visited[course] = 1;
        return true;
    }
}

@@ -38,23 +38,20 @@
 va.util.
-HashMap
+List
 ;%0A%0Apubli
@@ -147,33 +147,21 @@
         
-HashMap%3CInteger, 
+List%3C
 ArrayLis
@@ -176,29 +176,108 @@
 r%3E%3E 
-m
+gr
 ap
+h
  = new 
-HashMap
+ArrayList%3C%3E();%0A        for (int i = 0; i %3C numCourses; i++) graph.add(new ArrayList
 %3C%3E()
+)
 ;%0A  
@@ -386,107 +386,13 @@
     
-if (!map.containsKey(prerequisite%5B1%5D)) map.put(prerequisite%5B1%5D, new ArrayList%3C%3E());%0A            map
+graph
 .get
@@ -560,35 +560,37 @@
 vel(i, visited, 
-m
+gr
 ap
+h
 )) return false;
@@ -700,25 +700,13 @@
 ed, 
-HashMap%3CInteger, 
+List%3C
 Arra
@@ -721,19 +721,21 @@
 teger%3E%3E 
-m
+gr
 ap
+h
 ) %7B%0A    
@@ -860,89 +860,8 @@
 -1;%0A
-        ArrayList%3CInteger%3E tmp = map.get(course);%0A        if (tmp != null) %7B%0A    
     
@@ -890,11 +890,25 @@
 e : 
-tmp
+graph.get(course)
 ) %7B%0A
@@ -911,36 +911,32 @@
 ) %7B%0A            
- 
-   
 if (!travel(next
@@ -956,11 +956,13 @@
 ed, 
-m
+gr
 ap
+h
 )) r
@@ -970,38 +970,24 @@
 turn false;%0A
-            %7D%0A
         %7D%0A  

update DFS Solution for #207