1. Code

class Solution:
    def swapNodes(self, head: ListNode, k: int) -> ListNode:
        nodes = []
        cur = head
        while cur != None: # store all nodes in the list
            nodes.append(cur)
            cur = cur.next
        nodes[k-1].val, nodes[len(nodes)-k].val = nodes[len(nodes)-k].val, nodes[k-1].val # swap the value of the corresponding location nodes
        return head

2. Result

        Runtime : 1032 ms(91.39%), Memory usage : 48.9 MB(31.79%)
        (Runtime can be different by a system even if it is a same code.)

Check out the my GitHub repo for more info on the code. If you have questions, you can leave a reply on this post.