Transform linked list to adjacency list

Last updated: September 16, 2025

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. 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. The input will be the head of the linked list, and the output should be the adjacency list.

Optiver
Coding & Algorithms
Machine Learning Engineer
Optiver
September 16, 2025
Machine Learning Engineer
Phone Screen
Coding & Algorithms
Easy

13

4

2,311 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. 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. The input will be the head of the linked list, and the output should be the adjacency list.

This coding problem is frequently asked during Phone Screen at Optiver. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Optiver expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Identify the correct data structure and algorithm for the problem
  • Write clean, bug-free code with proper variable naming
  • Analyze time and space complexity correctly
  • Handle basic edge cases (empty input, single element)
  • Communicate your thought process while coding
Key Topics to Cover
Graph algorithms and traversal
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Time and space complexity analysis
Hash maps and frequency counting
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
  • What if the input doesn't fit in memory?
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • How would you test this solution thoroughly?
  • How would you parallelize this solution?
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

The problem requires transforming a singly linked list into an adjacency list representation of a graph where each node corresponds to a linked list node and its connections are represented by the nex...

Approach
  1. Initialize an empty list adjacency_list to store the adjacency lists for each node.
  2. Create a reference variable current starting at the head of the linked list.
  3. While current is not nul...

Submit Your Answer
Markdown supported

Related Questions