Skip to content

Commit

Permalink
Solution for Leedcode problem 2 and 21
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamdp authored and Shubham Patil committed Oct 18, 2019
1 parent 7059985 commit 4a7480e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
4 changes: 3 additions & 1 deletion leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
51 changes: 51 additions & 0 deletions leetcode/src/2.c
Original file line number Diff line number Diff line change
@@ -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 changes: 60 additions & 0 deletions leetcode/src/21.c
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 4a7480e

Please sign in to comment.