📜  使用递归将给定的数字添加到存储在链接列表中的数字中

📅  最后修改于: 2021-05-04 13:44:41             🧑  作者: Mango

给定一个表示整数的链表,其中每个节点是所表示整数的数字。任务是将给定的数字N添加到表示的整数中。
例子:

方法:这里讨论了解决此问题的迭代方法。在本文中,将讨论一种递归方法。
这个想法是递归地遍历LinkedList,直到到达最后一个节点。到达最后一个节点后,将N的值添加到它。添加后,如果该值大于9,则将进位和设置模式(数字%10)值保留为节点值,并将进位添加到先前的堆栈帧节点,并继续直到从堆栈中清除所有堆栈帧为止。
如果在清除了所有堆栈帧之后仍然有一个进位,则使用此值创建一个新节点,该节点将是链接列表的新头,指向前一个头。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
 
// Node class contains value
// and next node reference
struct ListNode
{
    int value;
    ListNode* next;
};
 
// To store the carry
int carry = 0;
void addNewValue(ListNode*, int);
 
// Function that calls the recursive method
// addNewValue to add a digit to the
// number represented as the linked list
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
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
void printList(ListNode* node)
{
  while (node != NULL)
  {
    cout << node->value << " -> ";
    node = node->next;
  }
  cout<<"NULL";
}
 
// Driver code
int main()
{
 
  // 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 rutvik_56


Java
// 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);
    }
}


Python
# Python implementation of the approach
 
# Node class contains value
# and next node reference
class ListNode:
    def __init__(self, new_data):
        self.value = new_data
        self.next = None
 
# To store the carry
carry = 0
 
# Function that calls the recursive method
# addNewValue to add a digit to the
# number represented as the linked list
def addValue(head, addValue):
 
    global carry
     
    # Add the digit recursively
    addNewValue(head, addValue)
 
    # If there is a carry after the addition
    if (carry != 0) :
 
        # Create a node
        newHead = ListNode(0)
 
        # 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 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
def addNewValue(head,addValue):
     
    global carry
 
    # If it is the last node in the list
    if (head.next == None) :
 
        # Add the digit
        val = head.value + addValue
 
        # Find the carry if any
        head.value = val % 10
        carry = int(val / 10)
     
    else :
 
        # Preserve the current node's value and call
        # the recursive function for the next node
        val = head.value
        addNewValue(head.next, addValue)
        val = val + carry
        head.value = val % 10
        carry = int(val / 10)
         
# Utility function to print the linked list
def printList(node):
     
    while (node != None) :
        print(node.value ,end= " -> ")
        node = node.next
         
    print("None")
     
# Driver code
 
# Create the linked list 9 -> 9 -> 3 -> None
head = ListNode(0)
head.value = 9
head.next = ListNode(0)
head.next.value = 9
head.next.next = ListNode(0)
head.next.next.value = 3
head.next.next.next = None
 
# Digit to be added
n = 7
head = addValue(head, n)
 
printList(head)
 
# This code is contributed by Arnab Kundu


C#
// 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


输出:
1 -> 0 -> 0 -> 0 -> NULL