1721. Swapping Nodes in a Linked List
Description
You are given the head of a linked list, and an integer k.
Return the head of the linked list after swapping the values of the kth node from the beginning and thekth node from the end (the list is 1-indexed ).
Example 1:

Example 2:
Constraints:
The number of nodes in the list is
n.1 <= k <= n <= 1050 <= Node.val <= 100
Tags
Linked List
Solution
Perform the slow-fast pointers strategy to find the value of the kth node from the end. Swap it with the value of __ the kth node from the beginning.
Complexity
Time complexity:
Space complexity:
Code
Last updated
Was this helpful?