Search code examples
pythonsingly-linked-list

How to Add 1 to a number represented as linked list using Python In the below code format?


I have a linked list with 1999 which I reversed to get 9991. However I am getting None as an output when calling add_one function for the below singly linked list coding exercise. I supposed to be getting 0002 after applying add_one function to 9991. And once I will apply reverse function it will be 2000. Kindly help me with the below code as to where it is going wrong?

class Node:
    def __init__(self, value):
        self.value= value
        self.next = None
        
class LinkedList:
    def __init__(self,value):
        new_node=Node(value)
        self.head=new_node
        self.tail=new_node
        self.length=1
        
    def print_list(self):
        temp = self.head
        while temp is not None:
            print(temp.value)
            temp = temp.next   
        
        
    def append(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node
        self.length += 1
        return True        
    
    def reverse_list(self):
        temp=self.head
        self.head=self.tail
        self.tail=temp
        after = temp.next
        before=None
        for _ in range(self.length):
            after=temp.next
            temp.next=before
            before=temp
            temp=after
            
    def add_one(self):
        carry=0
        prev=None
        self.head.value+=1
        while (self.head!=None) and (self.head.value>9 or carry>0):
            prev=self.head
            self.head.value+=carry
            carry=self.head.value//10
            self.head.value=self.head.value%10
            self.head=self.head.next


        if carry>0:
            prev.next=Node(carry)
        return prev.next
            
        
my_linked_list = LinkedList(1)
my_linked_list.append(9)
my_linked_list.append(9)
my_linked_list.append(9)
my_linked_list.print_list()
my_linked_list.reverse_list()
my_linked_list.print_list()
my_linked_list.add_one()
my_linked_list.print_list()

Solution

  • You need to modify your add_one function to the following:

    def add_one(self):
        carry = 1  # Start with a carry of 1
        prev = None  # Keep track of the previous node
        current = self.head  # Start from the head of the linked list
    
        while current is not None:
            prev = current  # Update the previous node
            new_value = current.value + carry  # Add the value of the current node with the carry
            carry = new_value // 10  # Calculate the new carry
            current.value = new_value % 10  # Update the value of the current node with the remainder
            current = current.next  # Move on to the next node
    
        if carry > 0:
            # If there's still a carry left, create a new node for it
            new_node = Node(carry)
            prev.next = new_node  # Link the new node with the previous node
            self.length += 1  # Increase the length of the linked list
    
        return prev  # Return the previous node
    

    This will ensure it adds one to the linked list when it is reversed.