Python coding

Unlink the k-th node from the back

Write remove_kth_from_end(head, k): remove the k-th node counting from the end of a singly linked list (k = 1 is the last node) and return the first node of the result, or None if nothing is left. The list has at least one node and 1 ≤ k ≤ its length. Each ListNode has val and next.

Aim for a single pass, without counting the nodes first, and unlink the node instead of building a new list.

The tests call the provided run_remove_kth_from_end(values, k), which builds the list from values and returns the values of the list you return.

  • run_remove_kth_from_end([1,2,3,4,5], 2) → [1,2,3,5]
  • run_remove_kth_from_end([1], 1) → []
  • run_remove_kth_from_end([1,2], 1) → [1]
Ctrl/⌘ + Enter runs · Tab indents
Hint 1

Use two pointers with a fixed gap of k nodes: when the front one reaches the last node, the back one sits just before the node to remove.

Hint 2

Start both at a dummy node placed before the head. Then removing the first node (k equal to the length) is the same move as any other: trail.next = trail.next.next.

Solution and why it works
pythonO(n) · O(1)
# --- your code ---
def remove_kth_from_end(head: "ListNode | None", k: int) -> "ListNode | None":
    dummy = ListNode(0, head)            # a node before the head: removing the head is no special case
    lead = trail = dummy
    for _ in range(k):                   # open a gap of k nodes
        lead = lead.next
    while lead.next is not None:         # slide the gap until lead is on the last node
        lead = lead.next
        trail = trail.next
    trail.next = trail.next.next         # trail sits just before the k-th node from the end
    return dummy.next


# --- provided: do not edit ---
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


def build_list(values: list[int]) -> ListNode | None:
    head = None
    for v in reversed(values):
        head = ListNode(v, head)
    return head


def to_nodes(head: ListNode | None, limit: int) -> list[ListNode]:
    out = []
    while head is not None:
        if len(out) == limit:
            raise ValueError("the result has more nodes than the input: is there a cycle?")
        out.append(head)
        head = head.next
    return out


def run_remove_kth_from_end(values: list[int], k: int) -> list[int]:
    head = build_list(values)
    given = to_nodes(head, len(values))
    left = to_nodes(remove_kth_from_end(head, k), len(values))
    if not {id(n) for n in left} <= {id(n) for n in given}:
        raise ValueError("unlink one node instead of building a new list")
    return [n.val for n in left]

Start both pointers at a dummy node before the head and move lead k nodes ahead. Then advance both until lead is on the last node: the gap is still k, so trail stops right before the k-th node from the end, and one link skips it. One pass, O(1) memory. The dummy matters when k equals the length: the node to remove is the head, and with nothing before it you need a special case. Return dummy.next, not head.