Exchange the Front Element with the k-th From the Back
Linked ListsAuto-graded · 18 test casesLast updated
Tests index arithmetic and edge-case handling by swapping the first element of a sequence with its k-th element counted from the end, including the case where both are the same element.
You are given a sequence of integers values, which represents a singly linked list read from front to back, and an integer k. Counting from the back of the sequence, the last element is the 1st from the end, the one before it is the 2nd from the end, and so on.
Produce a new sequence in which the first element and the k-th element from the end have exchanged positions. All other elements stay exactly where they were.
Rules:
- Exchange positions, not "values by equality": if the sequence contains duplicates, the swap still concerns the first position and the position that is k-th from the end.
- If the k-th element from the end is itself the first element (that happens exactly when
kequals the length of the sequence), the sequence is returned unchanged. - Do not modify the input list; return a new list.
In a linked-list setting, you would do this by relinking nodes in O(1) extra memory rather than copying values. Here the function receives and returns plain Python lists, and the result must be the same as that relinking would produce.
Constraints
1 <= k <= len(values)valuescontains at least one element- Elements are integers (duplicates are allowed)
- The returned list must have the same length as
values
Examples
Example 1
Input: swap_front_with_kth_from_back([1, 2, 3, 4, 5, 6], 1)
Output: [6, 2, 3, 4, 5, 1]
Example 2
Input: swap_front_with_kth_from_back([1, 2, 3, 4, 5, 6], 2)
Output: [5, 2, 3, 4, 1, 6]
Example 3
Input: swap_front_with_kth_from_back([1, 2, 3, 4, 5, 6], 5)
Output: [2, 1, 3, 4, 5, 6]
Hints
Hint 1
Convert 'k-th from the end' into a zero-based index from the front. For a sequence of length n, what index is it?
Hint 2
The first element sits at index 0. If the target index is also 0, there is nothing to do; otherwise exactly two positions change.
Hint 3
Copy the input before swapping so the caller's list is not mutated. For the pointer version, a pointer running k steps ahead of another finds the target in one pass, and the case where the target directly follows the head needs separate handling.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
With n = len(values), the k-th element from the end has zero-based index t = n - k.
- If
t == 0(sok == n), the target is the first element already, so the result is just a copy of the input. - Otherwise, swap the elements at index
0and indextin a copy of the list.
Checking with [1, 2, 3, 4, 5, 6] (n = 6):
| k | t = n - k | Result |
|---|---|---|
| 1 | 5 | [6, 2, 3, 4, 5, 1] |
| 2 | 4 | [5, 2, 3, 4, 1, 6] |
| 5 | 1 | [2, 1, 3, 4, 5, 6] |
| 6 | 0 | unchanged |
Why it is correct
The k-th element from the end is, by definition, the one with exactly k - 1 elements after it, which is index n - k. A swap of two distinct positions changes only those two positions, so every other element is untouched. When the two positions coincide, swapping would be a no-op, and the explicit check makes this clear. Duplicates are irrelevant because the swap is by position.
Linked-list perspective
On real nodes, you would run a fast pointer k steps ahead, then advance prev, cur and fast together until fast is None. At that point cur is the target and prev its predecessor. Then handle three cases: cur is the head (return unchanged), prev is the head (adjacent nodes, rewire without creating a self-loop), and the general case (save both successors, then relink). The outcome equals the index-based swap above.
Complexity
- Time: O(n), dominated by copying the list (the swap itself is O(1)).
- Space: O(n) for the returned copy; the pointer-based version needs only O(1) extra.
Reference solution
from typing import List
def swap_front_with_kth_from_back(values: List[int], k: int) -> List[int]:
result = values[:]
target = len(result) - k
if target != 0:
result[0], result[target] = result[target], result[0]
return result