969. Build Adjacency List from Edge List

EasyGraphsRepresentationGraph

Given an undirected graph with n nodes labeled 0..n-1 and a list of edges, build its adjacency list: for each node, the sorted list of its neighbors. Return the adjacency list as a nested array where entry i lists node i's neighbors in ascending order. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Nested array — sorted neighbors of each node.

Examples

Example 1
Input: {"n":5,"edges":[[0,1],[0,2],[1,3],[2,4]]}
Output: [[1,2],[0,3],[0,4],[1],[2]]
Explanation: Each node's neighbors, sorted.
Example 2
Input: {"n":1,"edges":[]}
Output: [[]]
Explanation: Single isolated node.
Example 3
Input: {"n":2,"edges":[[0,1]]}
Output: [[1],[0]]
Explanation: One edge both ways.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →