-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumLists.py
More file actions
50 lines (46 loc) · 1.37 KB
/
sumLists.py
File metadata and controls
50 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def addTwoNumbers(self, ll1, ll2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
res_head = None
carry = 0
while ll1 and ll2:
result = ll1.val + ll2.val + carry
carry = 0
if result > 9:
carry = 1
if not res_head:
res_head = ListNode(result % 10)
ptr_add = res_head
else:
ptr_add.next = ListNode(result % 10)
ptr_add = ptr_add.next
ll2 = ll2.next
ll1 = ll1.next
while ll1:
result = ll1.val+carry
carry = 0
ptr_add.next = ListNode(result % 10)
if result > 9:
carry = 1
ptr_add = ptr_add.next
ll1 = ll1.next
while ll2:
result = ll2.val+carry
carry = 0
ptr_add.next = ListNode(result % 10)
if result > 9:
carry = 1
ptr_add = ptr_add.next
ll2 = ll2.next
if carry != 0:
ptr_add.next = ListNode(carry)
return res_head