-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolutionNormal.py
More file actions
135 lines (106 loc) · 4.11 KB
/
SolutionNormal.py
File metadata and controls
135 lines (106 loc) · 4.11 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# ============================================================
# Add Two Numbers - Full Runnable Solution
# ============================================================
# Problem:
# Given two numbers represented as reversed linked lists,
# return their sum as a reversed linked list.
#
# Example:
# Input: l1 = 2->4->3 (342)
# l2 = 5->6->4 (465)
# Output: 7->0->8 (807)
# ============================================================
# ── Node definition ─────────────────────────────────────────
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# ── Helper: build linked list from a plain list ─────────────
def build_linked_list(numbers):
"""Convert a Python list to a linked list.
Example: [2, 4, 3] → 2->4->3
"""
dummy = ListNode(0)
current = dummy
for num in numbers:
current.next = ListNode(num)
current = current.next
return dummy.next
# ── Helper: convert linked list back to a plain list ────────
def linked_list_to_list(node):
"""Convert a linked list back to a Python list.
Example: 2->4->3 → [2, 4, 3]
"""
result = []
while node:
result.append(node.val)
node = node.next
return result
# ── Main solution ────────────────────────────────────────────
def addTwoNumbers(l1, l2):
"""
Add two numbers represented as reversed linked lists.
Args:
l1 (ListNode): Head of first linked list
l2 (ListNode): Head of second linked list
Returns:
ListNode: Head of result linked list (also reversed)
"""
dummy = ListNode(0) # placeholder node
current = dummy # pointer to build result
carry = 0 # carry from previous addition
while l1 or l2 or carry:
v1 = l1.val if l1 else 0 # digit from l1
v2 = l2.val if l2 else 0 # digit from l2
total = v1 + v2 + carry # sum of both digits + carry
carry = total // 10 # carry for next iteration
digit = total % 10 # digit to store in result
current.next = ListNode(digit) # attach new node
current = current.next # move pointer
l1 = l1.next if l1 else None # advance l1
l2 = l2.next if l2 else None # advance l2
return dummy.next # return result (skip dummy)
# ── Test cases ───────────────────────────────────────────────
def run_tests():
test_cases = [
{
"description": "Basic case: 342 + 465 = 807",
"l1": [2, 4, 3],
"l2": [5, 6, 4],
"expected": [7, 0, 8],
},
{
"description": "With carry at end: 999 + 1 = 1000",
"l1": [9, 9, 9],
"l2": [1],
"expected": [0, 0, 0, 1],
},
{
"description": "Both zeros: 0 + 0 = 0",
"l1": [0],
"l2": [0],
"expected": [0],
},
{
"description": "Different lengths: 99 + 1 = 100",
"l1": [9, 9],
"l2": [1],
"expected": [0, 0, 1],
},
]
all_passed = True
for i, test in enumerate(test_cases, 1):
l1 = build_linked_list(test["l1"])
l2 = build_linked_list(test["l2"])
result = linked_list_to_list(addTwoNumbers(l1, l2))
passed = result == test["expected"]
status = "✅ PASS" if passed else "❌ FAIL"
print(f"Test {i}: {status} | {test['description']}")
if not passed:
print(f" Expected: {test['expected']}")
print(f" Got: {result}")
all_passed = False
print()
print("All tests passed! 🎉" if all_passed else "Some tests failed. 😬")
if __name__ == "__main__":
run_tests()