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
Input: (head = [1, 2, 3, 4, 5]), (k = 2);
Output: [1, 4, 3, 2, 5];
Solution
Great solution found here
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var swapNodes = function (head, k) {
//Set fast, second, and first to head
let fast = head,
second = head,
first = head;
//For k - 1 times
for (let i = 0; i < k - 1; i++) {
fast = fast.next;
}
first = fast;
while (fast.next != null) {
fast = fast.next;
second = second.next;
}
let temp = first.val;
first.val = second.val;
second.val = temp;
return head;
};