Skip to content

Commit

Permalink
Adding recusrsive approach to "21. Merge Two Sorted Lists"
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamdp authored Oct 7, 2019
1 parent 90bb2aa commit 58a77bd
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions leetcode/src/21.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Iterative approach */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *list = NULL;
struct ListNode *tmp = NULL;
Expand Down Expand Up @@ -38,3 +39,18 @@ struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
return NULL;
}


/* Recursive approach */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
if(!l1)
return l2;t
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 58a77bd

Please sign in to comment.