Skip to content

Commit aed9746

Browse files
authored
Create addTwoNumbers.py
1 parent a2ee4f3 commit aed9746

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Kangli/LinkedList/addTwoNumbers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class ListNode(object):
2+
def __init__(self, x):
3+
self.val = x
4+
self.next = None
5+
6+
class Solution(object):
7+
def addTwoNumbers(self, l1, l2):
8+
dummy = cur = ListNode(0)
9+
carry = 0
10+
while l1 or l2 or carry:
11+
if l1:
12+
carry += l1.val
13+
l1 = l1.next
14+
if l2:
15+
carry += l2.val
16+
l2 = l2.next
17+
cur.next = ListNode(carry%10)
18+
cur = cur.next
19+
carry //= 10
20+
return dummy.next
21+
s = Solution()
22+
l1 = ListNode(2)
23+
l1.next = ListNode(4)
24+
l2 = ListNode(5)
25+
l2.next = ListNode(6)
26+
s.addTwoNumbers(l1, l2)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy