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).

&nbsp;
Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [1,4,3,2,5]


Example 2:

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]


&nbsp;
Constraints:


	The number of nodes in the list is n.
	1 &lt;= k &lt;= n &lt;= 105
	0 &lt;= Node.val &lt;= 100


class Solution:
    def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        tot = 0 # initialise total
        Head = head
        while Head: # count total nodes
            Head = Head.next # move forward
            tot += 1 # incerse count by one for each node
        one, two = None, None # two pointers of one and two
        count = 1 # we're initialising to one because we have one based index for swapping
        Head = head # regain original head to traverse
        while Head:
            if one and two: break # if we have both one and two then break loop to save time
            if count == k: # if from forward we reach at node k then it's our first node
                one = Head
            if count == (tot-k+1): # if from backward we reach to node k then save it
                two = Head
            Head = Head.next # move further
            count += 1 # increse count
        one.val, two.val = two.val, one.val # now swap values
        return head # return head
class Solution {
    public ListNode swapNodes(ListNode head, int k) {
        ListNode fast = head;
        ListNode slow = head;
        ListNode first = head, second = head;

        // Put fast (k-1) nodes after slow
        for(int i = 0; i < k - 1; ++i)
            fast = fast.next;

        // Save the node for swapping
        first = fast;

        // Move until the end of the list
        while(fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }

        // Save the second node for swapping
        // Note that the pointer second isn't necessary: we could use slow for swapping as well
        // However, having second improves readability
        second = slow;

        // Swap values
        int temp = first.val;
        first.val = second.val;
        second.val = temp;

        return head;
    }
}
class Solution {
public:
    ListNode* swapNodes(ListNode* head, int k) {
        
        // declare a dummy node
        
        ListNode* dummy = new ListNode(0);
        
        // point dummy -> next to head
        
        dummy -> next = head;
        
        // declare a tail pointer and point to dummy
        
        ListNode* tail = dummy;
        
        // move the curr pointer (k - 1) times
        
        // this will maintain a gap of (k - 1) between curr and tail pointer
        
        ListNode* curr = head;
        
        while(k > 1)
        {
            curr = curr -> next;
            
            k--;
        }
        
        // store the address in start pointer
        
        ListNode* start = curr;
        
        // maintaing a gap of (k - 1) between curr and tail, move both pointer
        
        while(curr)
        {
            tail = tail -> next;
            
            curr = curr -> next;
        }
        
        // store the address of kth node from end
        
        ListNode* end = tail;
        
        // swap the values
        
        swap(start -> val, end -> val);
        
        // dummy -> next will be head
        
        return dummy -> next;
    }
};
var swapNodes = function(head, k) {
    let A = head, B = head, K, temp
    for (let i = 1; i < k; i++) A = A.next
    K = A, A = A.next
    while (A) A = A.next, B = B.next
    temp = K.val, K.val = B.val, B.val = temp
    return head
};
Swapping Nodes in a Linked List