Advertisement
satishfrontenddev4

Untitled

Jan 5th, 2024
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the highest order digit is at the head of the list.
  3.  
  4. Write a function that adds the two numbers and returns the sum as a linked list.
  5.  
  6. Input format:
  7. First line contains N, number of elements in the first linked list.
  8.  
  9. Next line contains N space separated integers, elements of that linked list.
  10.  
  11. First line contains M, number of elements in the second linked list.
  12.  
  13. Next line contains M space separated integers, elements of that linked list.
  14.  
  15. Output format
  16. Print the sum of the given numbers.
  17.  
  18. Sample Input 1
  19. 3
  20.  
  21. 7 1 6
  22.  
  23. 3
  24.  
  25. 5 9 2
  26.  
  27. Sample Output 1
  28. 2 1 9
  29.  
  30. Explanation
  31. 617 + 295 = 912
  32.  
  33. Sample Input 2
  34. 2
  35.  
  36. 4 9
  37.  
  38. 2
  39.  
  40. 0 5
  41.  
  42. Sample Output 2
  43. 4 4 1
  44.  
  45. Explanation
  46. 94 + 50 = 144
  47.  
  48. Sample Input 3
  49. 2
  50.  
  51. 1 0
  52.  
  53. 1
  54.  
  55. 2
  56.  
  57. Sample Output 3
  58. 3 0
  59.  
  60. Explanation
  61. That is, 01 + 2 = 03. Note that we retain the preceding 0 as well.
  62.  
  63. Constraints
  64. 1 <= M, N <= 10^5
  65.  
  66. */
  67.  
  68.  
  69. /*
  70. class ListNode{
  71.     constructor(val){
  72.         this.val = val;
  73.         this.next = null;
  74.     }
  75. */
  76.  
  77. function getSum(str1,n,str2,m){
  78.       let carry=0,i=0,j=0,str='';
  79.       while(i<n&&j<m){
  80.             let sum=parseInt(str1[i])+parseInt(str2[i])
  81.             +carry;
  82.             carry =parseInt(sum/10);
  83.             sum=parseInt(sum%10);
  84.             str=str+(sum).toString();
  85.             i++;
  86.             j++;
  87.       }
  88.  
  89.       while(i<n){
  90.             let sum=parseInt(str1[i])+carry;
  91.             carry =parseInt(sum/10);
  92.             sum=parseInt(sum%10);
  93.             str=str+(sum).toString();
  94.             i++;
  95.       }
  96.  
  97.       while(j<m){
  98.             let sum=parseInt(str2[j])+carry;
  99.             carry =parseInt(sum/10);
  100.             sum=parseInt(sum%10);
  101.             str=str+(sum).toString();
  102.             j++;
  103.       }
  104.       if(carry>0){
  105.             str=str+(carry).toString();
  106.       }
  107.       return str;
  108.  
  109. }
  110.  
  111. function getFinalList(str){
  112.       let head=null;
  113.       let pointer=null;
  114.       let i=0;
  115.       while(i<str.length){
  116.             const newNode=new ListNode(parseInt(str[i]));
  117.             if(head==null){
  118.                   head=newNode;
  119.                   pointer=head;
  120.             }else{
  121.                   pointer.next=newNode;
  122.                   pointer=pointer.next;
  123.             }
  124.             i++;
  125.       }
  126.       return head;
  127.  
  128. }
  129. /**
  130.  * @param {ListNode} head1
  131.  * @param {ListNode} head2
  132.  * @return {ListNode}
  133.  */
  134. function sumLists1(head1, head2) {
  135.       let str1='',str2='',str='',head=null,ptr;
  136.       ptr=head1;
  137.       while(ptr!=null){
  138.             str1=str1+(ptr.val).toString();
  139.             ptr=ptr.next;
  140.       }
  141.  
  142.       ptr=head2;
  143.       while(ptr!=null){
  144.             str2=str2+(ptr.val).toString();
  145.             ptr=ptr.next;
  146.       }
  147.  
  148.       str=getSum(str1,str1.length,str2,str2.length);
  149.       head=getFinalList(str);
  150.     return head;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement