Skip to content

Commit de973e3

Browse files
bug fix for add two numbers
1 parent b3117b4 commit de973e3

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

.idea/workspace.xml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Leetcode/add_two_numbers.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44

55
def add_two_numbers(l1: Node, l2: Node) -> Node:
6+
"""
7+
Add two numbers stored as Linked List
8+
:param l1: Linked List 1
9+
:param l2: Linked List 2
10+
:return: Head of the added Linked List
11+
12+
Time Complexity: O(max(l1,l2))
13+
"""
614
head = None
715
current_node = None
816
carry = 0
@@ -17,11 +25,11 @@ def add_two_numbers(l1: Node, l2: Node) -> Node:
1725
l2 = l2.next
1826

1927
# case 2: When one of the nodes is None
20-
elif l1 is None:
28+
elif (l1 is None) and (l2 is not None):
2129
total += l2.data
2230
l2 = l2.next
2331

24-
elif l2 is None:
32+
elif (l1 is not None) and (l2 is None):
2533
total += l1.data
2634
l1 = l1.next
2735

0 commit comments

Comments
 (0)