Back to problems

Swap the K-th Node from the End with the Head in a Singly Linked List

Algorithm · Amplitude · Medium

Given the head of a singly linked list and a positive integer k, your task is to exchange the positions of the first node (the head) with the node that is k places from the end of the list. The swap must be done in-place by altering the next pointers of the involved nodes; simply swapping values is not allowed. You are to use only a constant amount of extra memory — no arrays, hash tables, or other auxiliary data structures that grow with the list size. The linked list nodes…

Loading the rest of the question…