<p>You are given the <code>head</code> of a linked list, and an integer <code>k</code>.</p>

<p>Return <em>the head of the linked list after <strong>swapping</strong> the values of the </em><code>k<sup>th</sup></code> <em>node from the beginning and the </em><code>k<sup>th</sup></code> <em>node from the end (the list is <strong>1-indexed</strong>).</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/linked1.jpg" style="width: 400px; height: 112px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5], k = 2
<strong>Output:</strong> [1,4,3,2,5]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> head = [7,9,6,6,7,8,3,0,9,5], k = 5
<strong>Output:</strong> [7,9,6,6,8,7,3,0,9,5]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the list is <code>n</code>.</li>
	<li><code>1 &lt;= k &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= Node.val &lt;= 100</code></li>
</ul>

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        val = []
        
        while head:
            val.append(head.val)
            head = head.next
        temp = val[k-1]
        val[k-1] = val[-k]
        val[-k] = temp
        prev = None
        head = None
        for v in val:
            cur = ListNode(v)
            if head is None: 
                head = cur
            if prev:
                prev.next = cur
            prev = cur
        return head
        # i = 1
        # node = head
        # while node:
        #     if i == k-1:
        #         prev1 = node
        #     node = node.next
        #     i += 1
        
        # j = i - 2*k 
        # prev2 = prev1 
        # # print(i, j, prev1.val)
        # if j == 1:
        #     #prev1->a->b->nxt
        #     a = prev1.next
        #     b = a.next
        #     nxt = b.next
        #     prev1.next = b
        #     b.next = a
        #     a.next = nxt
        #     return head
        # while j > 0:
        #     prev2 = prev2.next
        #     j -= 1 
        # # print(i, j, prev2.val)
        # next1, next2 = prev1.next.next, prev2.next.next
        # first, last = prev1.next, prev2.next

        # prev1.next = last
        # last.next = next1
        # prev2.next = first
        # first.next = next2

        # return head
51500