Skip to content

Commit

Permalink
Time: 4 ms (91.89%), Space: 14.9 MB (50.17%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
PrachetShah committed Apr 13, 2023
1 parent 14ea7d7 commit 11d2ec6
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions 0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,36 @@
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if(list1 == NULL)
if(list1 == NULL){
return list2;

if(list2 == NULL)
}
if(list2 == NULL){
return list1;

ListNode * ptr = list1;
if(list1 -> val > list2 -> val){
ptr = list2;
list2 = list2 -> next;
}
else {
list1 = list1 -> next;
ListNode* ptr = list1;
if(list1->val > list2->val){
ptr = list2;
list2 = list2->next;
}else{
list1 = list1->next;
}
ListNode *curr = ptr;
while(list1 && list2) {
if(list1 -> val < list2 -> val){
curr->next = list1;
list1 = list1 -> next;
}
else{

ListNode* curr = ptr;
while(list1 && list2){
if(list1->val > list2->val){
curr->next = list2;
list2 = list2 -> next;
list2 = list2->next;
}else{
curr->next = list1;
list1 = list1->next;
}
curr = curr -> next;
curr = curr->next;
}
if(!list1){
curr->next = list2;
}else{
curr->next = list1;
}
if(!list1)
curr -> next = list2;
else
curr -> next = list1;

return ptr;

}
};

0 comments on commit 11d2ec6

Please sign in to comment.