Build the Neighbor Lists
A network has n nodes numbered 0 to n - 1. Each entry of links is a pair [a, b]: a link between nodes a and b that works in both directions. No link is listed twice.
Return a list with one entry per node: entry i is the list of the nodes linked to node i, in increasing order. A node without links gets an empty list.
Example 1
n 4
links [[0,2],[1,0]]
returns [[1,2],[0],[0],[]]
Node 0 is linked to 1 and 2. Node 1 only to 0, node 2 only to 0, and node 3 to nobody.
Example 2
n 2
links [[1,0]]
returns [[1],[0]]
One link: each of the two nodes has the other as its neighbor.
Example 3
n 3
links []
returns [[],[],[]]
No links, so every list is empty.
Constraints
1 <= n <= 120 <= len(links) <= 30- a link joins two different nodes, and no two links join the same pair
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Graphs problems
- Can You Walk ItBeginner
- Count the LinksBeginner
- Dead EndsBeginner
- Degree of Every NodeBeginner
- Everyone Knows EveryoneBeginner
- Friends in CommonBeginner
- Is There a RoadBeginner
- Next DoorBeginner