Graph using adjacency list in Java
Use Java LinkedList class

Almost always prefer built-in data structures to hand-rolled. For instance, your insertion routine takes O(n) time while LinkedList::addLast takes O(1) times.

Also, use an ArrayList instead of an array to hold the lists due to problems with generic arrays in Java.

Write a Graph class

A class offers a great opportunity to re-organize your code. It makes it easy to separate initialization and adding edges.

Correctness issues

Validate the input to addEdge. Throw an exception if either to or from is not in the appropriate range.

Style issues


indent and capitalize properly
write fewer comments: only when something need explaining
write shorter comments: use sentence fragments


import java.util.ArrayList;
import java.util.LinkedList;

public class Graph {
    private ArrayList&lt;LinkedList&lt;Integer&gt;&gt; adjacencyList;

    public Graph(int numberNodes) {
        adjacencyList = new ArrayList&lt;LinkedList&lt;Integer&gt;&gt;(numberNodes);
        for (int i = 0; i &lt; numberNodes; i++) {
            adjacencyList.add(new LinkedList&lt;Integer&gt;());
        }
    }

    private boolean isValidNode(int index) {
        return index &gt;= 0 &amp;&amp; index &lt; adjacencyList.size();
    }

    public void addEdge(int to, int from) {
        if (!isValidNode(to) || !isValidNode(from)) {
            throw new IndexOutOfBoundsException();
        }

        adjacencyList.get(from).addLast(to);
    }
}


import java.util.Scanner;

public class GraphRunner {    
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        Graph g = new Graph(5);

        for(int from=0; from &lt; 5; from++) {
            int n=sc.nextInt();
            for(int j=0; j &lt; n; j++) {
                int to=sc.nextInt();
                g.addEdge(to, from);
            }
        }
    }
}