###########################################################################
#                                                                         #
# Author: Daniel Mock                                                     #
#                                                                         #
# Purpose: To document progress in learning algorithms & data structures  #
#                                                                         #
# References: This question was generated by leetcode.com. The solution   #
# to the question was generated by Daniel Mock.                           #
#                                                                         #
###########################################################################

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
        #Take care of empty list case
        if head is None: return head

        #create dummy node for edge cases
        dummy = ListNode(None)
        dummy.next = head

        #create 2 pointers for start & end of reverse sub-list
        node = dummy.next
        prev = dummy

        #array to help reverse by using pop()
        arr = []

        #step through list until first link of reverse sub-list
        for _ in range(m-1):
            prev = node
            node = node.next
        new_start = prev

        #keep stepping until last link of reverse sub-list
        #append nodes to arr as you step through
        for _ in range(n-m+1):
            if node is not None:
                arr.append(node)
                node = node.next

        #pop links and build new reversed sub-list
        while len(arr) > 0:
            new_start.next = arr.pop()
            new_start = new_start.next
        #add end of original list to end of reversed list
        new_start.next = node

        #return head
        return dummy.next
