diff --git a/Week_01/id_114/mergeTwoLists.c b/Week_01/id_114/mergeTwoLists.c new file mode 100644 index 00000000..10504f2f --- /dev/null +++ b/Week_01/id_114/mergeTwoLists.c @@ -0,0 +1,32 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { + struct ListNode* head; + struct ListNode* res; + res = (struct ListNode*)malloc(sizeof(struct ListNode)); + head = res; + + while(l1 && l2){ + if(l1->val < l2->val){ + res->next = l1; + l1 = l1->next; + }else{ + res->next = l2; + l2 = l2->next; + } + res = res->next; + } + + if(l1){ + res->next = l1; + }else{ + res->next = l2; + } + + return head->next; +} diff --git a/Week_01/id_114/swapPairs.c b/Week_01/id_114/swapPairs.c new file mode 100644 index 00000000..ab080346 --- /dev/null +++ b/Week_01/id_114/swapPairs.c @@ -0,0 +1,24 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +struct ListNode* swapPairs(struct ListNode* head) { + struct ListNode* temp; + + if(!head){ + return NULL; + } + if(!head->next){ + return head; + } + + temp = (struct ListNode*)malloc(sizeof(struct ListNode)); + + temp = head->next; + head->next=swapPairs(temp->next); + temp->next=head; + return temp; +}