Merge pull request #425 from shubhamdpatil/master

Solution for Leedcode problem 2 and 21
This commit is contained in:
Hai Hoang Dang 2019-10-18 17:14:00 -07:00 committed by GitHub
commit 3f9ebd1b5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 114 additions and 1 deletions

View File

@ -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|

51
leetcode/src/2.c Normal file
View File

@ -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;
}

60
leetcode/src/21.c Normal file
View File

@ -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;
}
}