Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Edges to Adjacency List Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Edges to Adjacency List

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given the nodes and edges of a graph, calculate the adjacency list for the graph.

You have a graph with n nodes indexed from 0 to n-1. You also have m bi-directional edges each connecting a couple of nodes.

You have to return the adjacency list for the given graph.

Approach

An adjacency list is a way of representing graphs, where each node corresponds to a list of nodes, to which it is connected.

  • Create a 2-D array having n rows.
  • Traverse the given edges. Let's say there is an edge between nodes i and j. Then add j in the ith row of the 2-D array and i in the jth row of the array.

Analysis

  • Time Complexity: O(m)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
vector<vector<int>> edgesToAdjList(int n, vector<vector<int>> edges) {
    vector<vector<int>> adjList(n);
	for (int i = 0; i < edges.size(); i++) {
		adjList[edges[i][0]].push_back(edges[i][1]);
		if (edges[i][0] == edges[i][1]) {
			continue;
		}
		adjList[edges[i][1]].push_back(edges[i][0]);
	}
	return adjList;
}
Java
class Solution {
    ArrayList<Integer>[] edgesToAdjList(int n, int[][] edges) {
		ArrayList<Integer>[] adjList = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			adjList[i] = new ArrayList<Integer>();
		}
		for (int i = 0; i < edges.length; i++) {
			adjList[edges[i][0]].add(edges[i][1]);
			if (edges[i][0] == edges[i][1]) {
				continue;
			}
			adjList[edges[i][1]].add(edges[i][0]);
		}
		return adjList;
    }
}
Related Content
Adjacency List to Adjacency Matrix
Adjacency Matrix to Adjacency List
BFS of a Graph
Capture Surrounded Regions
Clone a Graph
DFS of an Acyclic Graph
DFS of a Cyclic Graph
Flood Fill Image
Knight's Journey On A Chessboard
M-Coloring Problem
Number of Islands
Stepping Numbers
Valid Path
Word Search Board
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet