#include <stdio.h>
#include <stdlib.h>

struct node
{
  int value;
  struct node * next;
};

struct node *list1=NULL, *list2=NULL, *list=NULL, *tptr=NULL;

int main()
{
  int count1,count2,sum,carry=0,rem;
  char c;
  
  
  printf("Enter the number 1 for addition(press ENTER when done):\n");
  count1=0;
  
  while ( ( c = getchar() ) != '\n' )
  {
    
    tptr=(struct node *)malloc(sizeof(struct node));
    tptr->value=c-'0';
    
    tptr->next=list1;
    list1=tptr;
    count1++;
  }

  printf("Enter the number 2 for addition(press ENTER when done):\n");
  count2=0;
  
  while ( ( c = getchar() ) != '\n' )
  {
    tptr=(struct node *)malloc(sizeof(struct node));
    tptr->value=c-'0';
    
    tptr->next=list2;
    list2=tptr;
    count2++;
  }

  if ( count1 < count2 )
  {
    tptr=list1;
    list1=list2;
    list2=tptr;
  }

  list=list1;

  while ( list2 != NULL)
  {
    sum=list1->value+list2->value+carry;
    carry=sum/10;
    rem=sum%10;
    list1->value=rem;
    list1=list1->next;
    list2=list2->next;
  }
  
  while ( list1 != NULL)
  {
    
    if ( carry == 1)
    {
      sum=list1->value+carry;
      carry=sum/10;
      rem=sum%10;
      list1->value=rem;
    }
    list1=list1->next;
  }

  printf("The sum of the given two numbers is:\n");
  /*
  list2=NULL;
  
  while(list !=NULL)
  {
  tptr=list->next;
  list->next=list2;
  list2=list;
  list=tptr;
  }
  
  */
  if(carry==1)
  printf("%d", carry);
  
  while ( list != NULL)
  {
    printf("%d",list->value);
    list=list->next;
  }
  printf("\n");

  return 0;

}
    

     