Add two numbers represented by linked lists | Set 2

本文介绍了一种通过递归方法计算两个链表表示的数字之和的方法,不修改原始链表,并且不使用额外的空间。通过获取链表长度,将较短的链表前移相应位置,然后从右到左逐位相加,同时处理进位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: http://www.geeksforgeeks.org/sum-of-two-linked-lists/


Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. It is not allowed to modify the lists. Also, not allowed to use explicit extra space (Hint: Use Recursion).

Example 

Input:
  First List: 5->6->3  // represents number 563
  Second List: 8->4->2 //  represents number 842
Output
  Resultant list: 1->4->0->5  // represents number 1405

We have discussed a solution here which is for linked lists where least significant digit is first node of lists and most significant digit is last node. In this problem, most significant node is first node and least significant digit is last node and we are not allowed to modify the lists. Recursion is used here to calculate sum from right to left.

Following are the steps.
1) Calculate sizes of given two linked lists.
2) If sizes are same, then calculate sum using recursion. Hold all nodes in recursion call stack till the rightmost node, calculate sum of rightmost nodes and forward carry to left side.
3) If size is not same, then follow below steps:
….a) Calculate difference of sizes of two linked lists. Let the difference be diff
….b) Move diff nodes ahead in the bigger linked list. Now use step 2 to calculate sum of smaller list and right sub-list (of same size) of larger list. Also, store the carry of this sum.
….c) Calculate sum of the carry (calculated in previous step) with the remaining left sub-list of larger list. Nodes of this sum are added at the beginning of sum list obtained previous step.

Following is C implementation of the above approach.

// A recursive program to add two linked lists
 
#include <stdio.h>
#include <stdlib.h>
 
// A linked List Node
struct node
{
     int data;
     struct node* next;
};
 
typedef struct node node;
 
/* A utility function to insert a node at the beginning of linked list */
void push( struct node** head_ref, int new_data)
{
     /* allocate node */
     struct node* new_node = ( struct node*) malloc ( sizeof ( struct node));
 
     /* put in the data  */
     new_node->data  = new_data;
 
     /* link the old list off the new node */
     new_node->next = (*head_ref);
 
     /* move the head to point to the new node */
     (*head_ref)    = new_node;
}
 
/* A utility function to print linked list */
void printList( struct node *node)
{
     while (node != NULL)
     {
         printf ( "%d  " , node->data);
         node = node->next;
     }
     printf ( "\n" );
}
 
// A utility function to swap two pointers
void swapPointer( node** a, node** b )
{
     node* t = *a;
     *a = *b;
     *b = t;
}
 
/* A utility function to get size of linked list */
int getSize( struct node *node)
{
     int size = 0;
     while (node != NULL)
     {
         node = node->next;
         size++;
     }
     return size;
}
 
// Adds two linked lists of same size represented by head1 and head2 and returns
// head of the resultant linked list. Carry is propagated while returning from
// the recursion
node* addSameSize(node* head1, node* head2, int * carry)
{
     // Since the function assumes linked lists are of same size,
     // check any of the two head pointers
     if (head1 == NULL)
         return NULL;
 
     int sum;
 
     // Allocate memory for sum node of current two nodes
     node* result = (node *) malloc ( sizeof (node));
 
     // Recursively add remaining nodes and get the carry
     result->next = addSameSize(head1->next, head2->next, carry);
 
     // add digits of current nodes and propagated carry
     sum = head1->data + head2->data + *carry;
     *carry = sum / 10;
     sum = sum % 10;
 
     // Assigne the sum to current node of resultant list
     result->data = sum;
 
     return result;
}
 
// This function is called after the smaller list is added to the bigger
// lists's sublist of same size.  Once the right sublist is added, the carry
// must be added toe left side of larger list to get the final result.
void addCarryToRemaining(node* head1, node* cur, int * carry, node** result)
{
     int sum;
 
     // If diff. number of nodes are not traversed, add carry
     if (head1 != cur)
     {
         addCarryToRemaining(head1->next, cur, carry, result);
 
         sum = head1->data + *carry;
         *carry = sum/10;
         sum %= 10;
 
         // add this node to the front of the result
         push(result, sum);
     }
}
 
// The main function that adds two linked lists represented by head1 and head2.
// The sum of two lists is stored in a list referred by result
void addList(node* head1, node* head2, node** result)
{
     node *cur;
 
     // first list is empty
     if (head1 == NULL)
     {
         *result = head2;
         return ;
     }
 
     // second list is empty
     else if (head2 == NULL)
     {
         *result = head1;
         return ;
     }
 
     int size1 = getSize(head1);
     int size2 = getSize(head2) ;
 
     int carry = 0;
 
     // Add same size lists
     if (size1 == size2)
         *result = addSameSize(head1, head2, &carry);
 
     else
     {
         int diff = abs (size1 - size2);
 
         // First list should always be larger than second list.
         // If not, swap pointers
         if (size1 < size2)
             swapPointer(&head1, &head2);
 
         // move diff. number of nodes in first list
         for (cur = head1; diff--; cur = cur->next);
 
         // get addition of same size lists
         *result = addSameSize(cur, head2, &carry);
 
         // get addition of remaining first list and carry
         addCarryToRemaining(head1, cur, &carry, result);
     }
 
     // if some carry is still there, add a new node to the fron of
     // the result list. e.g. 999 and 87
     if (carry)
         push(result, carry);
}
 
// Driver program to test above functions
int main()
{
     node *head1 = NULL, *head2 = NULL, *result = NULL;
 
     int arr1[] = {9, 9, 9};
     int arr2[] = {1, 8};
 
     int size1 = sizeof (arr1) / sizeof (arr1[0]);
     int size2 = sizeof (arr2) / sizeof (arr2[0]);
 
     // Create first list as 9->9->9
     int i;
     for (i = size1-1; i >= 0; --i)
         push(&head1, arr1[i]);
 
     // Create second list as 1->8
     for (i = size2-1; i >= 0; --i)
         push(&head2, arr2[i]);
 
     addList(head1, head2, &result);
 
     printList(result);
 
     return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值