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

class Solution {
public:
    int Len(ListNode* head){
        if(!head) return 0;
        return 1 + Len(head->next); 
    }
    ListNode* swapNodes(ListNode* head, int k) {
        // case 1: 
        if(!head || !head->next) return head;
        int len = Len(head);    
        int lpos  = k;
        int rpos = len - k + 1;

        if(rpos < lpos) swap(lpos, rpos);    // used in case 4

        // case 2:
        if(lpos == rpos) return head;

        //case 3: 
        if(len == 2){
            ListNode* nextNode = head->next;
            nextNode->next = head;
            head->next = NULL;
            head = nextNode;
            return head;
        }

        // case 4: 
        if(lpos == 1){
            ListNode* lp = NULL;
            ListNode* ln = head;
            ListNode* rp = head;
            for(int i=0; i<rpos-2; i++) 
                rp = rp->next;
            ListNode* rn = rp->next;
            ListNode* rsave = rn->next;

            // swapping
            rn->next = ln->next;
            rp->next = ln;
            ln->next = rsave;
            head = rn;
            return head;
        }

        // case 5: noOfNodesBetweenSwapNodes = 0
        int noOfNodesBetweenSwapNodes = rpos - lpos - 1;
        if(noOfNodesBetweenSwapNodes == NULL){
            ListNode* lp = head;
            for(int i=0; i<lpos-2; i++)
                lp = lp->next;
            ListNode* ln = lp->next;

            ListNode* rp = head;
            for(int i=0; i<rpos-2; i++)
                rp = rp->next;
            ListNode* rn = rp->next;
            ListNode* rsave = rn->next;

            // swapping
            lp->next = rp->next;
            rn->next = rp;
            rp->next = rsave;
            return head; 
        }
        else {
            //case 6: noOfNodesBetweenSwapNodes >= 1
            ListNode* lp = head;
            for(int i=0; i<lpos-2; i++)
                lp = lp->next;
            ListNode* ln = lp->next;

            ListNode* rp = head;
            for(int i=0; i<rpos-2; i++)
                rp = rp->next;
            ListNode* rn = rp->next;
            ListNode* rsave = rn->next;

            //swapping
            lp->next = rn;
            rn->next = ln->next;
            rp->next = ln;
            ln->next = rsave;
            return head;
        }
    }
};
186500