Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Swapping Nodes in a Linked List

XPChallenge Points: 20
levelLevel: Medium

You are given the head of a singly linked list and a number k.
Your task is to swap the value of the node at position k from the start with the node at position k from the end of the list.
Remember the indexing starts from 1.

Example 1:

Input: head = [10, 20, 30, 40, 50], k = 1

Output: [50, 20, 30, 40, 10]

Explanation:

1st node from start is 10, 1st node from end is 50 → swap them.

Example 2:

Input: head = [2, 4, 6, 8], k = 3

Output: [2, 4, 8, 6]

Explanation:

3rd from start = 6, 3rd from end = 8 → swap.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result