Exchange Two Symmetric List Values

Quick Overview

Exchange Two Symmetric List Values. Given linked-list head and one-based position k, exchange the values stored in the kth node from the start and the kth node from the end. Return the existing list head after this value mutation.

Exchange Two Symmetric List Values

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Given linked-list `head` and one-based position `k`, exchange the values stored in the kth node from the start and the kth node from the end. Return the existing list head after this value mutation. List links need not change. ```cpp class Solution { public: ListNode* swapNodes(ListNode* head, int k); }; ``` ### Examples **Example 1:** ```text List values: [2,4,6,8], k = 1 Output list values: [8,4,6,2] Explanation: The first and last node values exchange. ``` **Example 2:** ```text List values: [3,7,9,1,5], k = 3 Output list values: [3,7,9,1,5] Explanation: Both selected positions refer to the middle node. ``` ### Constraints - The list has n nodes, with `1 <= k <= n <= 10^5`. - Each node value is in `[0,100]`. ### Clarifying Questions The original API receives a ListNode pointer and returns a ListNode pointer. Swap stored values, as the source states, rather than returning an array or rebuilding unrelated nodes. ```hint Inspect the structure Which existing nodes are selected by the two one-based positions, and which part of each node may change? ``` ### Reasoning checkpoints - Locate both one-based positions without an off-by-one error. - Mutate their values while preserving links and head identity. ### Optional extensions How would swapping the node objects themselves complicate adjacent-node and head cases?

Overview: Exchange Two Symmetric List Values. Given linked-list head and one-based position k, exchange the values stored in the kth node from the start and the kth node from the end. Return the existing list head after this value mutation.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given linked-list head and one-based position k, exchange the values stored in the kth node from the start and the kth node from the end. Return the existing list head after this value mutation. List links need not change.

class Solution {
public:
    ListNode* swapNodes(ListNode* head, int k);
};

Examples

Example 1:

List values: [2,4,6,8], k = 1
Output list values: [8,4,6,2]
Explanation: The first and last node values exchange.

Example 2:

List values: [3,7,9,1,5], k = 3
Output list values: [3,7,9,1,5]
Explanation: Both selected positions refer to the middle node.

Constraints

  • The list has n nodes, with 1 <= k <= n <= 10^5 .
  • Each node value is in [0,100] .

Clarifying Questions Guidance

The original API receives a ListNode pointer and returns a ListNode pointer. Swap stored values, as the source states, rather than returning an array or rebuilding unrelated nodes.

Reasoning checkpoints

  • Locate both one-based positions without an off-by-one error.
  • Mutate their values while preserving links and head identity.

Optional extensions

How would swapping the node objects themselves complicate adjacent-node and head cases?