Created
June 14, 2020 05:23
-
-
Save Irene-123/db67f7abece26b595d7df88de3d197db to your computer and use it in GitHub Desktop.
Merge two sorted Lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
class Solution { | |
public: | |
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
if(l1==NULL) return l2; | |
if(l2==NULL) return l1; | |
ListNode *p1=l1; | |
ListNode *p2=l2; | |
ListNode *head=NULL; | |
ListNode *cur=NULL; | |
while(p1&&p2){ | |
if(head==NULL){ | |
if(p1->val<=p2->val){ | |
head=cur=p1; | |
p1=p1->next; | |
}else{ | |
head=cur=p2; | |
p2=p2->next; | |
} | |
}else{ | |
if(p1->val<=p2->val){ | |
cur->next=p1; | |
cur=p1; | |
p1=p1->next; | |
}else{ | |
cur->next=p2; | |
cur=p2; | |
p2=p2->next; | |
} | |
} | |
} | |
if(p1) cur->next=p1; | |
if(p2) cur->next=p2; | |
return head; | |
} | |
}; | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment