Reputation: 45
My Aim is to do following
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
For doing this operation I am first creating a linklist, adding values to the created linklist. And using another function to add these two link lists. However I am getting stuck at the function add two nodes.
Code
class node:
def __init__(self, data=None):
self.data = data
self.next = None
class linklist:
def __init__(self):
self.head = node()
def append(self, data):
current_node = self.head
new_node = node(data)
while current_node.next != None:
current_node = current_node.next
current_node.next = new_node
def display(self):
elems = []
current_node = self.head
while current_node.next != None:
current_node = current_node.next
elems.append(current_node.data)
print(elems)
def getnode(self, index):
index_count = 0
current_node = self.head
while current_node.next != None:
current_node = current_node.next
if index_count == index:
return current_node.data
index_count += 1
class adding_lists:
def add_two_numbers(self, l1, l2, c=0):
for i in range(3):
value = l1.getnode(i)+l2.getnode(i)
print(value)
'''
whatever result comes inside value should be added to a new linklist
'''
# return new_node
l1 = linklist()
l1.append(2)
l1.append(4)
l1.append(3)
# l1.display()
l2 = linklist()
l2.append(5)
l2.append(6)
l2.append(4)
# l2.display()
add_object = adding_lists()
value = add_object.add_two_numbers(l1, l2)
I am getting stuck on class adding_lists, how do I add value to a new linked-list from within this adding_lists class
How should I append '''value''' generated in the add_two_numbers function (of adding_lists class) to form a completely new linklist data structure ?
Upvotes: 0
Views: 68
Reputation: 5757
You can try:
class node:
def __init__(self, data=None):
self.data = data
self.next = None
class linklist:
def __init__(self):
self.head = node()
def append(self, data):
current_node = self.head
new_node = node(data)
while current_node.next != None:
current_node = current_node.next
current_node.next = new_node
def display(self):
elems = []
current_node = self.head
while current_node.next != None:
current_node = current_node.next
elems.append(current_node.data)
print(elems)
def getnode(self, index):
index_count = 0
current_node = self.head
while current_node.next != None:
current_node = current_node.next
if index_count == index:
return current_node.data
index_count += 1
class adding_lists:
def add_two_numbers(self, l1, l2, c=0):
output = linklist()
x = l1.head.next
y = l2.head.next
carry = 0
while x is not None:
result = list(str(x.data + y.data + carry))
carry = int(result[0]) if len(result) > 1 else 0
output.append(int(result[-1]))
x = x.next
y = y.next
return output
l1 = linklist()
l1.append(2)
l1.append(4)
l1.append(3)
l1.display()
l2 = linklist()
l2.append(5)
l2.append(6)
l2.append(4)
l2.display()
add_object = adding_lists()
result = add_object.add_two_numbers(l1, l2)
result.display()
Upvotes: 1
Reputation: 1
I think this will work
def add_two_numbers(self, l1, l2, c=0):
value = linklist()
for i in range(3):
subTotal = l1.getnode(i) + l2.getnode(i) + c
c = 0
while subTotal > 9:
subTotal -= 10
c += 1
value.append(subTotal)
# print(value)
'''
whatever result comes inside value should be added to a new linklist
'''
return value
Then you just need to display the value with value.display()
.
In my machine i got this, output
Upvotes: 0