<p>Given the <code>head</code> of a linked list, reverse the nodes of the list <code>k</code> at a time, and return <em>the modified list</em>.</p>

<p><code>k</code> is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of <code>k</code> then left-out nodes, in the end, should remain as it is.</p>

<p>You may not alter the values in the list&#39;s nodes, only nodes themselves may be changed.</p>

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

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex2.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5], k = 3
<strong>Output:</strong> [3,2,1,4,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;= 5000</code></li>
	<li><code>0 &lt;= Node.val &lt;= 1000</code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow-up:</strong> Can you solve the problem in <code>O(1)</code> extra memory space?</p>

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        if k == 1: return head

        new_head, new_tail = None, None
        node = head

        while node != None:
            
            # check if it's a valid k-group
            k_node, i = node, 1
            last = None
            while i < k and k_node != None:
                k_node.last = last
                last = k_node
                k_node = k_node.next
                i += 1
            #print(f"k_node={k_node.val if k_node != None else None}",end=" ")
            k_node_next = k_node.next if k_node != None else None

            # valid k-group => reverse
            if i == k and k_node != None:
                #print(f"valid k-group")
                k_node.last = last
                if new_head == None:
                    new_head = k_node
                    new_tail = k_node
                #else: new_tail = k_node

                tmp = k_node
                for i in range(k):
                    #print(f"tmp={tmp.val if tmp != None else None} tail={new_tail.val if new_tail != None else None}, tail.next=tmp, tmp.next=None")
                    #if tmp == None: break
                    next_tmp = tmp.last
                    
                    if new_tail != None and new_tail != tmp: new_tail.next = tmp
                    tmp.next = None
                    new_tail = tmp

                    tmp = next_tmp    

            # no valid k-group => no reversing required
            else:
                #print(f"no valid k-group, new_tail={new_tail.val if new_tail != None else None}")
                if new_tail == None: return node
                else: new_tail.next = node
            
            # update pointer to current node (jump to beginning of next k-group)
            node = k_node_next

        return new_head
        
17700