Swap the Kth Experiment Arm From Each End by Relinking
Last updated: November 29, 2026
Quick Overview
In a singly linked list of experiment arms, swap the kth node from the start with the kth node from the end by relinking nodes, not by swapping their values, and return the head.
0
0
0 solved
In a singly linked list of experiment arms, swap the kth node from the start with the kth node from the end by relinking nodes, not by swapping their values, and return the head.
A Google technical screen variation where the value-swap shortcut is forbidden because nodes carry heavy payloads. The interviewer is testing careful predecessor tracking, especially when the two nodes are adjacent or the same.
What the Interviewer Expects
- Find both targets and their predecessors using lead-lag pointers
- Use a dummy node so swapping the head is uniform
- Handle the adjacent case where one node is the other's predecessor
- Handle the identical case when k is the middle
- O(n) time, O(1) space
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
- Allow swapping values instead. How much simpler is it?
- Swap the kth and kth-from-end in a doubly linked list.
- Reverse the segment between the two targets as well.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: head and 1 <= k <= n.
- Output: head with the two nodes exchanged by relinking.
- O(1) extra space....
High-Level Design
With a dummy in front, walk k - 1 steps to find p1, the predecessor of the front target a = p1.next. Then advance a lead pointer from a to the end while moving p2 from the dummy, so p2 ends ...