LeetCode Add Two Numbers

 Problem Description Link: https://leetcode.com/problems/add-two-numbers/description/


Accepted Solution

/*

 The solution technique is simple. You are given two linked lists. Find which is the larger or smaller. If both of them are similar in length, no problem. However, if you find any discrepancy in length, you have to insert extra zeros just back to the numbers. Finally, reverse each linked list, perform a fundamental summation, and store the results in another linked list, as you must return a linked list from the desired function.

For good understanding, I have utilized two arrays while performing summation. Although the process is straight, brainstorming is necessary for coding.

*/


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

void insert_data(struct ListNode **head, int data)
{
    struct ListNode *ptr, *tmp;
    tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
   
    ptr = *head;
   
    tmp->val = data;
    tmp->next = NULL;

    if(*head == NULL) {
        *head = tmp;
        return;
    }

    while(ptr->next != NULL) {
        ptr = ptr->next;
    }

    ptr->next = tmp;
}

struct ListNode *reverse_linked_list(struct ListNode *head)
{
    struct ListNode *temp = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *temp2 = (struct ListNode *)malloc(sizeof(struct ListNode));

    temp = NULL;
    temp2 = NULL;

    while (head != NULL)
    {
        temp2 = head->next;
        head->next = temp;
        temp = head;
        head = temp2;
    }

    head = temp;

    return head;
}

int FindLength(struct ListNode **head)
{
    struct ListNode *ptr = (struct ListNode*)malloc(sizeof(struct ListNode));

    int size = 0;

    if(head == NULL) {
        //cout << "Linked List is empty\n";
        return 0;
    }

    ptr = NULL;

    ptr = *head;

    while (ptr != NULL) {
        //cout << ptr->data << "; ";
        ptr = ptr->next;
        size += 1;
    }

    return size;
}

struct ListNode *insert_data_front(struct ListNode **head, int data)
{
    struct ListNode *ptr = (struct ListNode *)malloc(sizeof(struct ListNode));

    ptr->val = data;
    ptr->next = NULL;

    ptr->next = *head;
    *head = ptr;

    return *head;
}

struct ListNode* linked_list_sum(struct ListNode **head1, struct ListNode **head2, int len1, int len2)
{
    struct ListNode *ptr1 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *ptr2 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *sol = (struct ListNode *)malloc(sizeof(struct ListNode));

    int arr[len1+105], brr[len2+105];
    int alen = 0, blen = 0;

    ptr1 = *head1;
    ptr2 = *head2;
    sol = NULL;

    int sum = 0, carry = 0;

    while (ptr1 != NULL || ptr2!= NULL)
    {
        arr[alen] = ptr1->val;
        brr[blen] = ptr2->val;
        alen++;
        blen++;

        ptr1 = ptr1->next;
        ptr2 = ptr2->next;
    }
   
    for(int i=alen-1; i>=0; i--)
    {
        sum = arr[i] + brr[i] + carry;
        if(sum >= 10)
        {
            carry = sum / 10;
        }
        else
        {
            carry = 0;
        }
        //cout << "sum = " << sum%10 << " carry = " << carry << "\n";
        insert_data(&sol, sum%10);
    }
    if(carry > 0)
    {
        //cout << carry;
        insert_data(&sol, carry);
    }

    return sol;
}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    struct ListNode *ans = (struct ListNode *)malloc(sizeof(struct ListNode));
    int size_l1 = FindLength(&l1);
    int size_l2 = FindLength(&l2);
    if(size_l1 != size_l2)
    {
        if(size_l1 > size_l2)
        {
            int d = size_l1 - size_l2;

            for(int i=0; i<d; i++)
            {
                insert_data(&l2, 0);
            }
        }
        else
        {
            int d = size_l2 - size_l1;

            for(int i=0; i<d; i++)
            {
                insert_data(&l1, 0);
            }
        }
    }

    l1 = reverse_linked_list(l1);
    l2 = reverse_linked_list(l2);

    ans = linked_list_sum(&l1, &l2, size_l1, size_l2);

    return ans;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number