<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]:
        LLlist=[]
        current=head
        length=0
        while current:
            LLlist.append(current.val)
            length+=1
            current=current.next
        print(LLlist)
        if length%2!=0 and ((length//2)+1)==k:
            return head
        LLlist[k-1],LLlist[-1*k]=LLlist[-1*k],LLlist[k-1]
        print(LLlist)
        op_ll=ListNode()
        current=op_ll
        i=0
        while i<len(LLlist):
            current.next=ListNode(LLlist[i])
            current=current.next
            i+=1
        return op_ll.next
        # # print(n_k)
        # if (k==1 and length==2*k) or (k==length and length==n_k*2):
        #     first=head
        #     last=current
        #     last.next=first
        #     head.next=None
        #     head=last
        #     return head
            
        # current=head
        # i=1
        # while i<k-1 and i<n_k-1:
        #     current=current.next
        #     i+=1
        # prevk=current
        # kthNode=current.next
        # afterk=kthNode.next
        # if k==1:
        #     prevk=None
        #     kthNode=current
        #     afterk=kthNode.next
        # # print(prevk.val,kthNode.val,afterk.val)
        # if i==k-1:
        #     cond=n_k-1
        # else:
        #     cond=k-1
        # while i<cond:
        #     current=current.next
        #     i+=1
        # # print(current.val)
        # prevn_k=current
        # n_kthNode=current.next
        # aftern_k=n_kthNode.next
        
        # # print(prevn_k.val,n_kthNode.val,aftern_k.val)
        # if k!=n_k-1:
        #     if prevk:
        #         prevk.next=n_kthNode
        #     else:
        #         head=n_kthNode
        #     n_kthNode.next=afterk
        #     prevn_k.next=kthNode
        #     kthNode.next=aftern_k
        # else:
        #     prevk.next=n_kthNode
        #     n_kthNode.next=kthNode
        #     kthNode.next=aftern_k
        # return head



51100