-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsol.c
57 lines (51 loc) · 1.28 KB
/
sol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* createNode(int value) {
struct ListNode* node = calloc(1, sizeof(*node));
node->val = value;
return node;
}
struct ListNode* addNode(struct ListNode* rear, int value) {
struct ListNode* node = createNode(value);
if (rear == NULL) {
return node;
} else {
rear->next = node;
return node;
}
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode* curr1 = l1;
struct ListNode* curr2 = l2;
struct ListNode* final_list = NULL;
struct ListNode* rear = NULL;
int add = 0;
int number;
while (curr1 || curr2) {
number = 0;
if (curr1) {
number += curr1->val;
curr1 = curr1->next;
}
if (curr2) {
number += curr2->val;
curr2 = curr2->next;
}
number += add;
rear = addNode(rear, number % 10);
// memorise first node as the head of list
if (final_list == NULL) {
final_list = rear;
}
add = number / 10; // keep in mind for next sum
}
if (add > 0) {
addNode(rear, add);
}
return final_list;
}