0

leetcode problem: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    struct ListNode *head = malloc(sizeof(struct ListNode));
    head->next = NULL;
    struct ListNode *temp = head;
    int carry = 0;
    int x, y;
    while (l1 != NULL || l2 != NULL || carry != 0) {
        temp->next = malloc(sizeof(struct ListNode));
        int sum = (l1 != NULL ? l1->val : 0) + (l2 != NULL ? l2->val : 0) + carry;
        // if (l1 != NULL) x = l1->val;
        // if (l2 != NULL) y = l2->val;
        // int sum = x + y + carry;
        temp->next->val = (sum % 10);
        carry = sum / 10;
        temp->next->next = NULL;
        temp = temp->next;
        if (l1 != NULL) l1 = l1->next;
        if (l2 != NULL) l2 = l2->next;
    }
    temp->next = NULL;
    return (head->next);
}

This is my code and It was able to be passed. However, If I use variables x and y to get a sum of l1->val and l2->val-commenting "int sum = (l1 != NULL ? l1->val : 0) + (l2 != NULL ? l2->val : 0) + carry;" this part out , like

    // if (l1 != NULL) x = l1->val;
    // if (l2 != NULL) y = l2->val;
    // int sum = x + y + carry;

or

    x = l1 != NULL ? l1->val : 0;
    y = l2 != NULL ? l2->val : 0;

It causes a time limit exceeded error. Can anyone please explain what is wrong?

Hans
  • 49
  • 1
  • 7
  • you already calculate the sum, why are you trying to calculate it again. YOu should not be writing the newlist inside the read loop, you should write it in a second loop after you calculate the sum – pm100 Feb 21 '22 at 05:33
  • why do you not use the first node (head)? how will you free that node later when there is no prev ptr and you return head->next – AndersK Feb 21 '22 at 05:57
  • @pm100 oh, my question was that when I use the second and third conditional state instead of the first one(commenting. it), it's time limit exceeded! – Hans Feb 21 '22 at 06:18
  • @AndersK I've set temp node that has an address of head node as a dummy node to store information. I only use head node to return the starting linked list. Because It didn't give me errors in regards to not freeing allocated memory, I didn't consider freeing the memory. would it be better if I free temp node just before return state? – Hans Feb 21 '22 at 06:21
  • 1
    In the version with if statements, when corresponding node pointers are null, x and/or y are not initialised or contain previous garbage values instead of zeros – n. m. could be an AI Feb 21 '22 at 06:48
  • @n.1.8e9-where's-my-sharem. WOW YOU'RE SO RIGHT. when I initialize x and y, it works. But when l1 and l2 are not null, wouldn't x and y be initialized as 0 according to my second conditional statement? – Hans Feb 21 '22 at 07:24
  • oh I meant the third conditional statement -> "x = l1 != NULL ? l1->val : 0;" It initializes variable x when l1 is not a NULL but it generates same error as the second conditional statement. What am I missing? – Hans Feb 21 '22 at 08:53
  • Not sure why this happens. – n. m. could be an AI Feb 21 '22 at 11:23

0 Answers0