isOriginalHead
boolean 변수를 둬서, 원래 머리 즉, 새로운 꼬리는 next = None
으로 처리해줌 def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head:
global newHead
solution(head, True)
return newHead
else:
return None
def solution(thisNode, isOriginalHead):
global newHead
if not thisNode.next: # if None
newHead = thisNode
return newHead
preNode = solution(thisNode.next, False) # go down to tail
preNode.next = thisNode # when come up, reverse
if isOriginalHead:
thisNode.next = None
return thisNode