<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]:
        # time: O(n)
        # space: O(1)

        # edge case: if linked list is empty or contains only one node O(1)
        if not head or not head.next:
            return head

        # edge case: there's only two nodes in linked list O(1)
        if head.next.next is None:
            temp = head.next
            head.next.next = head
            head.next = None
            head = temp
            return head

        # find linked list's length & kth node from beginning O(n)
        begin = cursor = head
        begin_prev = test = None
        length = 0
        while cursor:
            # keep iterating until we find kth node from beginning O(1)
            if length < k - 1:
                # stores begin node's prev node O(1)
                if length == k - 2:
                    begin_prev = begin

                begin = begin.next
            
            # found begin node, set end node to head
            elif length == k - 1: 
                test = head.next
                print(cursor.val, test.val, begin.val)

            length += 1

            if test:
                test = test.next
                print(cursor.val)
            cursor = cursor.next

            

        # iterate to find kth node from the end O(n)
        end = head
        end_prev = None
        for i in range(length - k):
            if i == length - k - 1:
                end_prev = end
            end = end.next

        # swap two nodes O(1)
        # case 1: if kth node from beginning and kth node from the end is the same node O(1)
        # no swap needed
        if begin == end:
            head == end
        # case 2: k is 1 or k is length (swap head with end node)
        elif k == 1:
            head = end
            end.next = begin.next
            end_prev.next = begin
            begin.next = None
        elif k == length:
            head = begin
            begin.next = end.next
            begin_prev.next = end
            end.next = None
        # case 3: the two nodes are adjacent
        elif begin.next is end:
            if begin_prev:
                begin_prev.next = end
            begin.next = end.next
            end.next = end_prev
        # case 4: the two nodes are not adjacent
        else:
            begin_prev.next = end
            temp = end.next
            end.next = begin.next
            if end_prev:
                end_prev.next = begin
            begin.next = temp
        
        # return linked list O(1)
        return head
40900