1721 Swapping Nodes in a Linked List
Last updated
Last updated
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).
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Constraints:
The number of nodes in the list is n
.
1 <= k <= n <= 105
0 <= Node.val <= 100
这题一开始还以为是要sawp那个node,后来才发现swap value就好了。然后用快慢指针把back指针移动到正确位置。弄快慢指针的时候,顺便弄了front指针。最后swap就好了。