1721. Swapping Nodes in a Linked List
Last updated
Was this helpful?
Last updated
Was this helpful?
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 <= 105
0 <= Node.val <= 100
Linked List
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.
Time complexity:
Space complexity: