Problem 2: add two numbers

This commit is contained in:
Ivan R. 2022-11-17 21:13:04 +05:00
parent b7e708b5b7
commit e1dedc1464
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C
5 changed files with 149 additions and 0 deletions

View file

@ -0,0 +1 @@
../template/Makefile

View file

@ -0,0 +1,45 @@
#include <stdlib.h>
#include "lib.h"
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode* current = NULL;
struct ListNode* first_node = NULL;
int reminder = 0;
while (l1 != NULL || l2 != NULL || reminder != 0) {
// Read digits
int a = 0;
int b = 0;
if (l1 != NULL)
a = l1->val;
if (l2 != NULL)
b = l2->val;
// Move pointers to the next node
if (l1 != NULL)
l1 = l1->next;
if (l2 != NULL)
l2 = l2->next;
// Calculate the result
int digit = (a + b + reminder) % 10;
reminder = (a + b + reminder) / 10;
// Create the first node
if (current == NULL) {
current = malloc(sizeof(struct ListNode));
first_node = current;
}
// Create regular node
else {
current->next = malloc(sizeof(struct ListNode));
current = current->next;
}
// Save the result
current->val = digit;
current->next = NULL;
}
return first_node;
}

View file

@ -0,0 +1,8 @@
#pragma once
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2);

View file

@ -0,0 +1,86 @@
#include <stdlib.h>
#include <stdio.h>
#include "lib.h"
// Creates single node
struct ListNode* mkNode(int val) {
struct ListNode* node = malloc(sizeof(struct ListNode));
node->val = val;
node->next = NULL;
return node;
}
// Creates linked list
struct ListNode* mkList(int* nums, int length) {
struct ListNode* first = NULL;
struct ListNode* current = NULL;
for (int i=0; i<length; i++) {
// First node
if (current == NULL) {
current = mkNode(nums[i]);
first = current;
}
// Regular node
else {
current->next = mkNode(nums[i]);
current = current->next;
}
}
return first;
}
// Returns 1 if linked lists are equal.
// Returns 0 otherwise.
int cmpLists(struct ListNode* a, struct ListNode* b) {
while (a != NULL || b != NULL) {
// Both nodes exist
if (a == NULL || b == NULL)
return 0;
// Check values
if (a->val != b->val)
return 0;
// Go to next node
a = a->next;
b = b->next;
}
return 1;
}
// Free allocated memory
void freeLinkedList(struct ListNode* list) {
struct ListNode* current;
while (list != NULL) {
current = list;
list = list->next;
free(current);
}
}
int main() {
// First list
int a[] = {2, 4, 3};
struct ListNode* list_a = mkList(a, 3);
// Second list
int b[] = {5, 6, 4};
struct ListNode* list_b = mkList(b, 3);
// Expected sum
int c[] = {7, 0, 8};
struct ListNode* list_c = mkList(c, 3);
// Computed result
struct ListNode* res = addTwoNumbers(list_a, list_b);
printf("Test 1: %d\n", cmpLists(res, list_c));
freeLinkedList(list_a);
freeLinkedList(list_b);
freeLinkedList(list_c);
freeLinkedList(res);
return 0;
}

View file

@ -0,0 +1,9 @@
# 2. Add two numbers
* [Leetcode](https://leetcode.com/problems/add-two-numbers/)
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.