All problems
Adjacency List Build
Problem
Build an undirected adjacency list for n nodes labeled 0 to n-1 from a list of edges.
edges is a list of [u, v] pairs. Return a list of n lists, where the i-th list holds the neighbors of node i in ascending order.
Hint
Append each endpoint to the other node's list, then sort every list.
Test cases
case 1
in: [4,[[0,1],[1,2],[2,3]]]
expected: [[1],[0,2],[1,3],[2]]
case 2
in: [3,[[0,1],[0,2],[1,2]]]
expected: [[1,2],[0,2],[0,1]]
case 3
in: [2,[]]
expected: [[],[]]
case 4
in: [1,[]]
expected: [[]]
Starter codePython
def build_adjacency_list(n, edges):
# Your code here
pass4 test cases · run your code against them in the editor