File tree Expand file tree Collapse file tree 1 file changed +21
-17
lines changed
Expand file tree Collapse file tree 1 file changed +21
-17
lines changed Original file line number Diff line number Diff line change 1414# def __init__(self, val=0, next=None):
1515# self.val = val
1616# self.next = next
17+ # Definition for singly-linked list.
18+ # class ListNode:
19+ # def __init__(self, val=0, next=None):
20+ # self.val = val
21+ # self.next = next
1722class Solution :
1823 def addTwoNumbers (self , l1 : ListNode , l2 : ListNode ) -> ListNode :
19- head1 ,head2 = l1 ,l2
20- temp = ListNode (- 100 )
21- cur = temp
22- carry ,total = 0 ,0
23- while head1 or head2 :
24- a = head1 .val if head1 else 0
25- b = head2 .val if head2 else 0
26- cur_total = a + b + carry
27- carry = cur_total // 10
28- temp .next = ListNode (cur_total % 10 )
29- temp = temp .next
30- if head1 :
31- head1 = head1 .next
32- if head2 :
33- head2 = head2 .next
24+ prev = ListNode (- 100 )
25+ cur = prev
26+ total ,carry = 0 ,0
27+ while l1 or l2 :
28+ x = l1 .val if l1 else 0
29+ y = l2 .val if l2 else 0
30+ total = (x + y + carry ) % 10
31+ carry = (x + y + carry ) // 10
32+ cur .next = ListNode (total )
33+ cur = cur .next
34+ if l1 :
35+ l1 = l1 .next
36+ if l2 :
37+ l2 = l2 .next
3438 if carry :
35- temp .next = ListNode (carry )
36- return cur .next
39+ cur .next = ListNode (carry )
40+ return prev .next
You can’t perform that action at this time.
0 commit comments