Advertisement
yarin0600

MultiplicationBetweenTwoLinkedLists

Nov 9th, 2023
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. Node *multipleTwoNumbersRepresentedByLinkedList(Node *headOne, Node *headTwo)
  2. {
  3.    Node newDummyHead(INT_MAX);
  4.    Node *tailOfNewList = &newDummyHead;
  5.  
  6.    // all edge cases
  7.    if (!headOne || !headTwo)
  8.       return nullptr;
  9.    if (!(headOne->next) && headOne->val == 0)
  10.       return new Node(0);
  11.    if (!(headTwo->next) && headTwo->val == 0)
  12.       return new Node(0);
  13.  
  14.    // First of all: reverse both linkedlists
  15.    headOne = reverseLinkedList(headOne);
  16.    headTwo = reverseLinkedList(headTwo);
  17.  
  18.    Node *travelerOne = headOne;
  19.    Node *travelerTwo = headTwo;
  20.  
  21.    int currentValueOfMultiplication = 0;
  22.    int currentCarryOfMultiplication = 0;
  23.    int currentValueOfAddition = 0;
  24.    int currentCarryOfAddition = 0;
  25.  
  26.    int level = 1; // first level is '1'
  27.  
  28.    while (travelerTwo)
  29.    {
  30.       // reset before trying new multiplication
  31.       tailOfNewList = &newDummyHead;
  32.       travelerOne = headOne;
  33.       currentCarryOfMultiplication = 0;
  34.       currentCarryOfAddition = 0;
  35.  
  36.       for (int i = 0; i + 1 < level; ++i) // we are advancing level - 1 times here to consider the level everytime we start new level:
  37.       {
  38.          tailOfNewList->next = tailOfNewList->next ? tailOfNewList->next : new Node(0);
  39.          tailOfNewList = tailOfNewList->next;
  40.       }
  41.  
  42.       while (travelerOne)
  43.       {
  44.          tailOfNewList->next = tailOfNewList->next ? tailOfNewList->next : new Node(0);
  45.          tailOfNewList = tailOfNewList->next;
  46.  
  47.          currentValueOfMultiplication = travelerOne->val * travelerTwo->val + currentCarryOfMultiplication;
  48.  
  49.          currentCarryOfMultiplication = currentValueOfMultiplication / 10;
  50.          currentValueOfMultiplication %= 10;
  51.  
  52.          tailOfNewList->val = tailOfNewList->val + currentValueOfMultiplication + currentCarryOfAddition;
  53.          currentCarryOfAddition = tailOfNewList->val / 10;
  54.          tailOfNewList->val %= 10;
  55.  
  56.          travelerOne = travelerOne->next;
  57.       }
  58.  
  59.       travelerTwo = travelerTwo->next;
  60.       ++level;
  61.    }
  62.  
  63.    if (currentCarryOfAddition != 0)
  64.    {
  65.       tailOfNewList->next = new Node(currentCarryOfAddition);
  66.    }
  67.  
  68.    return reverseLinkedList(newDummyHead.next);
  69. }
  70.  
  71. Node *reverseLinkedList(Node *head)
  72. {
  73.    Node *prev, *curr, *next;
  74.    prev = nullptr;
  75.    curr = head;
  76.    while (curr)
  77.    {
  78.       next = curr->next;
  79.       curr->next = prev;
  80.       prev = curr;
  81.       curr = next;
  82.    }
  83.    return prev;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement