Transform linked list to adjacency list

Last updated: December 10, 2025

Quick Overview

Given a singly linked list where each node contains a value and a reference to its next node, transform the linked list into an adjacency list representation of a graph. Each node's value should serve as a key in the adjacency list, with its corresponding value being a list of all connected nodes (i.e., nodes that can be reached from the current node). The output should be a dictionary where each key is a node's value and the value is a list of adjacent node values.

Salesforce
Coding & Algorithms
Machine Learning Engineer
Salesforce
December 10, 2025
Machine Learning Engineer
Phone Screen
Coding & Algorithms
Medium

4

15

268 solved


Given a singly linked list where each node contains a value and a reference to its next node, transform the linked list into an adjacency list representation of a graph. Each node's value should serve as a key in the adjacency list, with its corresponding value being a list of all connected nodes (i.e., nodes that can be reached from the current node). The output should be a dictionary where each key is a node's value and the value is a list of adjacent node values.

Salesforce uses this problem in the Phone Screen to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Edge cases and input validation
Hash maps and frequency counting
Graph algorithms and traversal
Tree structures and recursion
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you test this solution thoroughly?
  • Can you solve this in a single pass?
  • Can you solve this iteratively instead of recursively (or vice versa)?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

In this problem, we need to convert a singly linked list into an adjacency list representation of a graph. The crucial observation here is that the linked list itself can be seen as a linear graph whe...

Approach
  1. Initialize a Dictionary: Create an empty dictionary to hold our adjacency list.
  2. Traverse the Linked List: Use a pointer to traverse the linked list starting from the head.
  3. **Build the...

Submit Your Answer
Markdown supported

Related Questions