Add the given digit to a number stored in a linked list using recursion

Given a linked list which represents an integer number where each node is a digit of the represented integer. The task is to add a given digit N to the represented integer.

Examples:

Input: LL = 9 -> 9 -> 3 -> NULL, N = 7
Output: 1 -> 0 -> 0 -> 0 -> NULL
993 + 7 = 1000



Input: LL = 2 -> 9 -> 9 -> NULL, N = 5
Output: 3 -> 0 -> 4 -> NULL

Approach: An iterative approach to solve this problem has been discussed here. In this article, a recursive approach will be discussed.

The idea is to traverse the LinkedList recursively until the last node is reached. Once the last node has been reached, add the value of N to it. After adding, if the value is more then 9 then keep the carry and set mode (digit % 10) value to the node value and add carry to the previous stack frame node and continue until all the stack frames are cleared from the stack.
If there is a carry after all the stack frames have been cleared then create a new node with this value which will be the new head of the linked list pointing to the previous head.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
  
// Node class contains value
// and next node reference
class ListNode {
    int value;
    ListNode next;
}
  
class GFG {
  
    // To store the carry
    private static int carry = 0;
  
    // Function that calls the recursive method
    // addNewValue to add a digit to the
    // number represented as the linked list
    public static ListNode addValue(ListNode head, int addValue)
    {
  
        // Add the digit recursively
        addNewValue(head, addValue);
  
        // If there is a carry after the addition
        if (carry != 0) {
  
            // Create a new node
            ListNode newHead = new ListNode();
  
            // Assign it with carry
            newHead.value = carry;
  
            // Make it point to the head of
            // the linked list
            newHead.next = head;
            carry = 0;
  
            // Make it the new head
            return newHead;
        }
  
        // If there's not carry then
        // return the previous head
        else {
            return head;
        }
    }
  
    // Recursive function to add a digit to the number
    // represented as the given linked list
    private static void addNewValue(ListNode head, int addValue)
    {
  
        // If it is the last node in the list
        if (head.next == null) {
  
            // Add the digit
            int val = head.value + addValue;
  
            // Find the carry if any
            head.value = val % 10;
            carry = val / 10;
        }
        else {
  
            // Preserve the current node's value and call
            // the recursive function for the next node
            int val = head.value;
            addNewValue(head.next, addValue);
            val = val + carry;
            head.value = val % 10;
            carry = val / 10;
        }
    }
  
    // Utility function to print the linked list
    private static void printList(ListNode node)
    {
        while (node != null) {
            System.out.print(node.value + " -> ");
            node = node.next;
        }
        System.out.print("NULL");
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Create the linked list 9 -> 9 -> 3 -> NULL
        ListNode head = new ListNode();
        head.value = 9;
        head.next = new ListNode();
        head.next.value = 9;
        head.next.next = new ListNode();
        head.next.next.value = 3;
        head.next.next.next = null;
  
        // Digit to be added
        int n = 7;
        head = addValue(head, n);
  
        printList(head);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
// Node class contains value
// and next node reference
public class ListNode 
{
    public int value;
    public ListNode next;
}
  
class GFG
{
  
    // To store the carry
    private static int carry = 0;
  
    // Function that calls the recursive method
    // addNewValue to add a digit to the
    // number represented as the linked list
    public static ListNode addValue(ListNode head, 
                                     int addValue)
    {
  
        // Add the digit recursively
        addNewValue(head, addValue);
  
        // If there is a carry after the addition
        if (carry != 0) 
        {
  
            // Create a new node
            ListNode newHead = new ListNode();
  
            // Assign it with carry
            newHead.value = carry;
  
            // Make it point to the head of
            // the linked list
            newHead.next = head;
            carry = 0;
  
            // Make it the new head
            return newHead;
        }
  
        // If there's not carry then
        // return the previous head
        else
        {
            return head;
        }
    }
  
    // Recursive function to add a digit to the number
    // represented as the given linked list
    private static void addNewValue(ListNode head, 
                                     int addValue)
    {
  
        // If it is the last node in the list
        if (head.next == null)
        {
  
            // Add the digit
            int val = head.value + addValue;
  
            // Find the carry if any
            head.value = val % 10;
            carry = val / 10;
        }
        else 
        {
  
            // Preserve the current node's value and call
            // the recursive function for the next node
            int val = head.value;
            addNewValue(head.next, addValue);
            val = val + carry;
            head.value = val % 10;
            carry = val / 10;
        }
    }
  
    // Utility function to print the linked list
    private static void printList(ListNode node)
    {
        while (node != null
        {
            Console.Write(node.value + " -> ");
            node = node.next;
        }
        Console.Write("NULL");
    }
  
    // Driver code
    public static void Main(String[] args)
    {
  
        // Create the linked list 9 -> 9 -> 3 -> NULL
        ListNode head = new ListNode();
        head.value = 9;
        head.next = new ListNode();
        head.next.value = 9;
        head.next.next = new ListNode();
        head.next.next.value = 3;
        head.next.next.next = null;
  
        // Digit to be added
        int n = 7;
        head = addValue(head, n);
  
        printList(head);
    }
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

1 -> 0 -> 0 -> 0 -> NULL


My Personal Notes arrow_drop_up

Software Engineer, an Algorithms enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : princiraj1992