topic:
Thought:
I originally wrote the stupidest way,把两个Linked转换成数字,Then add,再转换成Linked。But it’s strange that I was right when I was running locally,But it’s wrong when you submit,
What is deducted is a very strange oneprecompiled.listnode.ListNode
,But mine is'__main__.ListNode'
。
So I can only watch0x3fs answer。
Two node values each time`l1.val`,`l2.val`In -digit`carry`Add,Divide 10 The remaining number is the digital digit that the current node needs to be saved,Divide10The business is the new position value
Code implementation,There is a trick to simplify code:ifrecursion中发现l2
Length ratio ratiol1
Longer,So it can be exchangedl1
and l2
,ensure l1
Not an empty node,So as to simplify code logic。
Code:
1 | class Solution: |
1 | class Solution: |