blob: 079aafa46247a5f67825798b59005cff17b41e75 (
plain)
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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class LinkedList:
def __init__(self, value=0, next=None):
self.value = value
self.next = next
def add(self, other):
carry = 0
head = tail = LinkedList()
while self or other or carry:
sum = (self.value if self else 0) + \
(other.value if other else 0) + carry
tail.next = LinkedList(sum % 10)
tail = tail.next
carry = sum // 10
self = self.next if self else None
other = other.next if other else None
return head.next
def __str__(self):
node = self
values = []
while node:
values.append(str(node.value))
node = node.next
return ' -> '.join(values)
l1 = LinkedList(1, LinkedList(2, LinkedList(3)))
l2 = LinkedList(3, LinkedList(2, LinkedList(1)))
result = l1.add(l2)
print(result)
|