PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Amplitude

Swap kth-from-end node with head

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's understanding of singly linked list pointer manipulation, in-place node swapping with O(1) extra memory, and handling of edge cases such as when the k-th node from the end is the head.

  • medium
  • Amplitude
  • Coding & Algorithms
  • Software Engineer

Swap kth-from-end node with head

Company: Amplitude

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given the head of a singly linked list and an integer `k`, swap the **k-th node from the end** with the **head node**, using **O(1)** extra memory. Requirements: - Perform the swap by changing pointers (swap nodes), not by swapping values. - Assume `1 <= k <= n` where `n` is the list length. - If the k-th node from the end is already the head (i.e., `k == n`), return the list unchanged. Examples (list: `1 -> 2 -> 3 -> 4 -> 5 -> 6`): - `k = 1` => `6 -> 2 -> 3 -> 4 -> 5 -> 1` - `k = 2` => `5 -> 2 -> 3 -> 4 -> 1 -> 6` - `k = 5` => `2 -> 1 -> 3 -> 4 -> 5 -> 6` Return the new head of the linked list.

Quick Answer: This question evaluates a candidate's understanding of singly linked list pointer manipulation, in-place node swapping with O(1) extra memory, and handling of edge cases such as when the k-th node from the end is the head.

Related Interview Questions

  • Implement Employee Validation and Snake - Amplitude (medium)
  • Implement lossless dictionary encoding and decoding - Amplitude (medium)
Amplitude logo
Amplitude
Feb 25, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
1
0

Given the head of a singly linked list and an integer k, swap the k-th node from the end with the head node, using O(1) extra memory.

Requirements:

  • Perform the swap by changing pointers (swap nodes), not by swapping values.
  • Assume 1 <= k <= n where n is the list length.
  • If the k-th node from the end is already the head (i.e., k == n ), return the list unchanged.

Examples (list: 1 -> 2 -> 3 -> 4 -> 5 -> 6):

  • k = 1 => 6 -> 2 -> 3 -> 4 -> 5 -> 1
  • k = 2 => 5 -> 2 -> 3 -> 4 -> 1 -> 6
  • k = 5 => 2 -> 1 -> 3 -> 4 -> 5 -> 6

Return the new head of the linked list.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amplitude•More Software Engineer•Amplitude Software Engineer•Amplitude Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.