old_contents: """
  public ListNode reverseKGroup(ListNode head, int k) {
      ListNode curr = head;
      int count = 0;
      while (curr != null && count != k) { // find the k+1 node
          curr = curr.next;
          count++;
      }
      if (count == k) { // if k+1 node is found
          curr = reverseKGroup(curr, k); // reverse list with k+1 node as head
          // head - head-pointer to direct part, 
          // curr - head-pointer to reversed part;
          while (count-- > 0) { // reverse current k-group: 
              ListNode tmp = head.next; // tmp - next head in direct part
              head.next = curr; // preappending "direct" head to the reversed list 
              curr = head; // move head of reversed part to a new node
              head = tmp; // move "direct" head to the next node in direct part
          }
          head = curr;
      }
      return head;
  }
  """
  
new_contents: """
  public ListNode reverseKGroup(ListNode head, int k) {
      ListNode curr = head;
      int count = 0;
      while (curr != null && count != k) { // find the k+1 node
          curr = curr.next;
          count++;
      }
      if (count == k) { // if k+1 node is found
          curr = reverseKGroup(curr, k); // reverse list with k+1 node as head
          // head - head-pointer to direct part, 
          // curr - head-pointer to reversed part;
          while (count-- > 0) { // reverse current k-group: 
              ListNode tmp = head.next; // tmp - next head in direct part
              head.next = curr; // preappending "direct" head to the reversed list 
              curr = head; // move head of reversed part to a new node
              head = tmp; // move "direct" head to the next node in direct part
          }
          head = curr;
      }
      return head;
  }
  """
  
  
  # Definition for singly-linked list.
  # class ListNode(object):
  #     def __init__(self, x):
  #         self.val = x
  #         self.next = None
  
  class Solution(object):
      def reverseKGroup(self, head, k):
          """
          :type head: ListNode
          :type k: int
          :rtype: ListNode
          """
          cur = head
          count = 0
          while cur and count < k:
              cur = cur.next
              count += 1
          
          if count == k:
              cur = self.reverseKGroup(cur, k)
              
              while count > 0:
                  tmp = head.next
                  head.next = cur
                  cur = head
                  head = tmp
                  count -= 1
                  
              head = cur
          return head
  
subject: Update reverse_k.py