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
December 10, 20254
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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
- Initialize a Dictionary: Create an empty dictionary to hold our adjacency list.
- Traverse the Linked List: Use a pointer to traverse the linked list starting from the head.
- **Build the...