this is the code that I want to ask about.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
length_1 = 0
tmp_1 = l1
l1_true_val = 0
l1_end = False
length_2 = 0
tmp_2 = l2
l2_true_val = 0
l2_end = False
while(1):
if l1_end is False:
l1_true_val = l1_true_val + tmp_1.val * pow(10, length_1)
if tmp_1.next is not None:
tmp_1 = tmp_1.next
length_1 = length_1 + 1
else:
l1_end = True
if l2_end is False:
l2_true_val = l2_true_val + tmp_2.val * pow(10, length_2)
if tmp_2.next is not None:
tmp_2 = tmp_2.next
length_2 = length_2 + 1
else:
l2_end = True
if l1_end is True and l2_end is True:
break
output = l1_true_val + l2_true_val
devider = 10
if output == 0:
Out = ListNode(val=0)
return Out
Out = ListNode()
this_out = Out
while(output):
this_out.val = output % 10
output = output // 10
if output != 0:
nxt_out = ListNode()
this_out.next = nxt_out
this_out = this_out.next
#this_out = copy.copy(this_out.next)
return Out
I'm quite new to Python, but I've learned that = means shallow copy in Python.
If it is true, then this_out = this_out.next
should be same with this_out = copy.copy(this_out.next)
. But the output isn't the same.
How could this happen? I referred this doc