Swapping Nodes in a Linked List Problem

Swapping Nodes in a Linked List Problem — ExecCode Easy DSA Practice

Solve the Swapping Nodes in a Linked List problem on ExecCode. Free online easy DSA practice in Linked List. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Description You are given the head of a linked list, and an integer k. Your task is to compute the required answer and return the head of the linked list after swapping the values of the k^th node from the beginning and the k^th node from the end (the list is 1-indexed).

Examples

Input {"nums": [1, 2, 3, 4, 5], "k": 2}; Output [1, 4, 3, 2, 5]

Constraints

The number of nodes in the list is n. 1 <= k <= n <= 10^5 0 <= Node.val <= 100

Practice Swapping Nodes in a Linked List free on ExecCode. Browse DSA problems, topic map, and placement guides.