diff --git a/leetcode/README.md b/leetcode/README.md index d05e3cb2..fe701f66 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -6,10 +6,12 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | -|1|[Two Sum](https://leetcode.com/problems/two-sum/) | [C](./src/1.c)|Easy +|1|[Two Sum](https://leetcode.com/problems/two-sum/) | [C](./src/1.c)|Easy| +|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [C](./src/2.c)|Medium| |3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium| |4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [C](./src/4.c)|Hard| |20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy| +|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C](./src/21.c)|Easy| |24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium| |26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [C](./src/26.c)|Easy| |27|[Remove Element](https://leetcode.com/problems/remove-element/) | [C](./src/27.c)|Easy| diff --git a/leetcode/src/2.c b/leetcode/src/2.c new file mode 100644 index 00000000..d929ef5b --- /dev/null +++ b/leetcode/src/2.c @@ -0,0 +1,51 @@ +/* + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { + struct ListNode *head = NULL; + struct ListNode *walk = NULL; + struct ListNode *tmp = NULL; + + int carry = 0; + int val1 = 0; + int val2 = 0; + int val = 0; + + while(l1 != NULL || l2 != NULL || carry) { + val1 = 0; + val2 = 0; + val = 0; + + if(l1) { + val1 = l1->val; + l1 = l1->next; + } + + if(l2) { + val2 = l2->val; + l2 = l2->next; + } + + val = carry + val1 + val2; + carry = val / 10; + + tmp = malloc(sizeof(struct ListNode)); + tmp->val = val % 10; + tmp->next = NULL; + + if(!head) { + head = walk = tmp; + } else { + walk->next = tmp; + walk = walk->next; + } + } + + return head; +} + diff --git a/leetcode/src/21.c b/leetcode/src/21.c new file mode 100644 index 00000000..08f6f71a --- /dev/null +++ b/leetcode/src/21.c @@ -0,0 +1,60 @@ +/* + * Iterative approach + */ +struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { + struct ListNode *list = NULL; + struct ListNode *tmp = NULL; + + if (!l1) + return l2; + if (!l2) + return l1; + + if (l1 && l2) { + if (l1->val < l2->val) { + list = tmp = l1; + l1 = l1->next; + } else { + list = tmp = l2; + l2 = l2->next; + } + + while(l1 && l2) { + if (l1->val < l2->val) { + tmp->next = l1; + l1 = l1->next; + } else { + tmp->next = l2; + l2 = l2->next; + } + tmp = tmp->next; + } + + if (l1) + tmp->next = l1; + if (l2) + tmp->next = l2; + + return list; + } + + return NULL; +} + +/* + * Recursive approach + */ +struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { + if(!l1) + return l2; + if(!l2) + return l1; + if(l1->val < l2->val) { + l1->next = mergeTwoLists(l1->next, l2); + return l1; + } else { + l2->next = mergeTwoLists(l1, l2->next); + return l2; + } +} +