class Solution:
def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
up = False
order = []
cur = head
while cur.next != None:
order.append(cur)
cur = cur.next
order.append(cur)
while order:
cur = order.pop()
if cur.val * 2 >= 10:
cal = up
up = True
cur.val = (cur.val * 2 + cal) % 10
else:
cal = up
up = False
cur.val = cur.val * 2 + cal
if up == True:
new_head = ListNode(val=1, next=head)
return new_head
return head
무난하게...
리스트 안 쓰고 메모리 절약하는 방법도 있겠지만.. 귀찮아..
댓글 0