<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]:
        cnt = 0
        curr = head
        map = {}

        if not head:
            return None
        if not head.next:
            return head

        while curr:
            cnt += 1
            map[cnt] = curr
            curr = curr.next

            
        if cnt == 2:
            head.next.next = head
            head = head.next
            head.next.next = None
            return head

        if map[k].next == map[cnt-k+1]:
            temp = map[cnt-k+1].next
            if k-1 in map:
                map[k-1].next = map[cnt-k+1]
            else:
                head = map[cnt-k+1]
            map[cnt-k+1].next = map[k]
            map[k].next = temp
            return head

        
        
        
        
        head_is_k = False
        if head == map[k]:
            head_is_k = True

        if map[k].next == map[cnt-k+1]:
            map[k-1].next = map[cnt-k+1]
            map[k].next = map[cnt-k+1].next
            map[cnt-k+1].next = map[k]
            return head

        if map[cnt-k+1].next == map[k]:
            print(map[k].val)
            print(map[cnt-k+1].val)
            map[cnt-k].next = map[k]
            map[cnt-k+1].next = map[k].next
            map[k].next = map[cnt-k+1]
            return head

        temp = map[cnt-k+1].next
        map[cnt-k+1].next = map[k].next
        if k-1 in map:
            map[k-1].next = map[cnt-k+1]
        else:
            head = map[cnt-k+1]
        if cnt-k in map:
            map[cnt-k].next = map[k]
        else:
            head = map[k]
        map[k].next = temp

        if head_is_k:
            head = map[cnt-k+1]

        curr2 = head
        while curr2:
            print(curr2.val)
            curr2 = curr2.next

        return head
        

49300