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

#single pass
class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        node_mem = []
        if head.next is None: return
        while head:
            node_mem.append(head)
            head = head.next
        if n == 1:
            node_mem[-n-1].next = None
        elif n == len(node_mem):
            node_mem[0].next = None
            node_mem.pop(0)
        else:
            node_mem[-n-1].next = node_mem[-n+1]
        return node_mem[0]

'''
class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        if head is None: return head
        empty = ListNode(0)
        empty.next = head

        curr = head
        next = curr
        count = 0
        while curr is not None:
            curr = curr.next
            count +=1
        curr = empty
        for i in range(count - n):
            curr = curr.next

        curr.next = curr.next.next

        return empty.next

'''
