Swap Nodes in Linked List Problem

Swap Nodes in Linked List Problem — ExecCode Easy DSA Practice

Solve the Swap Nodes in 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. Input {"nums": [1], "k": 1}; Output 1. Input {"nums": [7, 9, 6, 6, 7], "k": 5}; Output 7 9 6 6 7

Constraints

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

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