Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
46 lines (37 loc) · 1.23 KB

File metadata and controls

46 lines (37 loc) · 1.23 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from typing import Optional
# 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]:
def getDigit(l1, l2) -> dict:
no1 = 0
if l1 != None:
no1 = l1.val
no2 = 0
if l2 != None:
no2 = l2.val
sumNo = sum(no1 + no2)
if sumNo >= 10:
dict["0"] = sumNo % 10
dict["1"] = 1
else:
dict["0"] = sumNo
dict["1"] = -1
return dict
reminder = 0
firstNode = ListNode()
resultNode = firstNode
while resultNode != None:
digiDict = getDigit(l1, l2)
resultNode.val = digiDict["0"] + reminder
if digiDict["1"] > 0:
reminder = digiDict["1"]
l1 = l1.next
l2 = l2.next
if l1 != None or l2 != None:
resultNode.next = ListNode()
resultNode = resultNode.next
return firstNode
Morty Proxy This is a proxified and sanitized view of the page, visit original site.