Reverse linked lists, including k-group

Quick Overview

This question evaluates understanding of linked list manipulation including pointer operations, in-place reversal techniques, recursion and stack-depth considerations, cycle detection, and time/space complexity analysis.

Reverse linked lists, including k-group

Company: NVIDIA

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: hard

Interview Round: HR Screen

Reverse a singly linked list. Provide iterative O(1)-space and recursive solutions, analyze time/space, and handle edge cases: empty list, one node, very long list (stack depth), and a sublist reversal between positions m and n. Then extend to reverse nodes in k groups with O(1) extra space, preserving leftover tail order. Explain how you would detect and reject cyclic lists before reversing.

Quick Answer: This question evaluates understanding of linked list manipulation including pointer operations, in-place reversal techniques, recursion and stack-depth considerations, cycle detection, and time/space complexity analysis.

|Home/Coding & Algorithms/NVIDIA
NVIDIA logo
NVIDIA
Oct 13, 2025, 9:49 PM
hardData ScientistHR ScreenCoding & Algorithms
6
0

Singly Linked List Reversal — Variants and Edge Cases

You are given a standard singly linked list with nodes of the form:

  • Node fields: value, next
  • Head pointer may be null (empty list).
  • Assume 1-indexed positions for m and n.

Implement and analyze the following:

A) Reverse Entire List

  1. Provide an iterative in-place solution that uses O(1) extra space.
  2. Provide a recursive solution.
  3. Analyze time and space complexity of both.
  4. Explicitly handle edge cases: empty list, single-node list, and very long lists (stack depth considerations for recursion).

B) Reverse a Sublist [m, n]

  • Reverse the nodes from position m to n (inclusive), in-place, and return the head. Assume 1 ≤ m ≤ n ≤ length of list.

C) Reverse Nodes in k-Groups

  • Reverse the list in contiguous groups of size k using O(1) extra space. If the final group has fewer than k nodes, leave that tail group as-is.

D) Cycle Detection Before Reversal

  • Explain and implement how to detect a cycle in the list and reject (do not attempt to reverse) if a cycle is present.

You may use pseudo-code or code in a language of your choice.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...