Trying to add two linked lists in Python, but the resulting Linked List is printing in scientific notation

It sounds like you are trying to add two linked lists in Python, but the resulting linked list is being printed in scientific notation. Here are a few things you can try to fix this issue:

Make sure that you are storing the values in the linked list nodes as integers, not floats. If the values are stored as floats, they may be displayed in scientific notation when printed.

Use the str() function to convert the values in the linked list nodes to strings before adding them to the resulting linked list. This will prevent them from being displayed in scientific notation.

Use the format() function to control the way the values are printed. For example, you can use format(value, '.0f') to print the value as a whole number without scientific notation.

Here is an example of how you might use these approaches to add two linked lists and print the result without scientific notation:

class Node:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def addTwoNumbers(l1, l2):
    result = Node()
    current = result
    carry = 0
    while l1 or l2 or carry:
        val1 = l1.val if l1 else 0
        val2 = l2.val if l2 else 0
        sum = val1 + val2 + carry
        carry = sum // 10
        current.next = Node(sum % 10)
        current = current.next
        l1 = l1.next if l1 else None
        l2 = l2.next if l2 else None
    return result.next

def printLinkedList(node):
    result = []
    while node:
        result.append(str(node.val))
        node = node.next
    print(' -> '.join(result))

l1 = Node(2, Node(4, Node(3)))
l2 = Node(5, Node(6, Node(4)))
result = addTwoNumbers(l1, l2)
printLinkedList(result)

Comments

Popular posts from this blog

In PHP how to split string on specific character and keep that character and keep that character separate ie in its own element of an array after

How to read a single row or of multiple txt files in python

Unable to deliver the push notification using parse server