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 the kth node from the end (the list is 1-indexed).
Constraints
The number of nodes in the list is
n.1 <= k <= n <= 1050 <= Node.val <= 100
Approach
Links
GeeksforGeeks
ProgramCreek
YouTube
Examples
Input: head = [1, 2, 3, 4, 5], k = 2
Output: [1, 4, 3, 2, 5]
Input: head = [7, 9, 6, 6, 7, 8, 3, 0, 9, 5], k = 5
Output: [7, 9, 6, 6, 8, 7, 3, 0, 9, 5]
Input: head = [1], k = 1
Output: [1]
Input: head = [1, 2], k = 1
Output: [2, 1]
Input: head = [1, 2, 3], k = 2
Output: [1, 2, 3]
Solutions
Follow up
Last updated
Was this helpful?