Transform linked list to adjacency list

Last updated: March 4, 2026

Quick Overview

Given a singly linked list where each node contains a value and a reference to its next node, transform this linked list into an adjacency list representation of a graph. The output should be a list of lists, where each inner list contains the values of the nodes that are directly connected to the corresponding node in the linked list. Each node in the linked list is treated as a vertex in the graph, and the connections between nodes represent edges.

Cruise
Coding & Algorithms
Software Engineer
Cruise
March 4, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

90

13

2,025 solved


Given a singly linked list where each node contains a value and a reference to its next node, transform this linked list into an adjacency list representation of a graph. The output should be a list of lists, where each inner list contains the values of the nodes that are directly connected to the corresponding node in the linked list. Each node in the linked list is treated as a vertex in the graph, and the connections between nodes represent edges.

Coding interviews at Cruise focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

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
Binary search and divide and conquer
Hash maps and frequency counting
Data structure selection and trade-offs
Edge cases and input validation
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
  • Can you optimize the space complexity of your solution?
  • How would you parallelize this solution?
  • What if the input doesn't fit in memory?
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

To transform a singly linked list into an adjacency list representation of a graph, we need to identify the connections between nodes. Each node in the linked list is treated as a vertex. Since the li...

Approach
  1. Initialize an empty list called adjacency_list to store the connections.
  2. Create a pointer current that points to the head of the linked list.
  3. While current is not None: a. Create an ...

Submit Your Answer
Markdown supported

Related Questions