Last active
August 29, 2015 14:08
-
-
Save sonald/367efbdc0becee379b81 to your computer and use it in GitHub Desktop.
leetcode: merge tow sorted lists
This file contains 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
// https://oj.leetcode.com/problems/merge-two-sorted-lists/ | |
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { | |
ListNode dummy {0}; | |
ListNode** tail = &dummy.next; | |
while (l1 && l2) { | |
ListNode* tmp = nullptr; | |
if (l1->val < l2->val) { | |
tmp = l1; l1 = l1->next; | |
} else { | |
tmp = l2; l2 = l2->next; | |
} | |
*tail = tmp; | |
tail = &tmp->next; | |
} | |
ListNode* tmp = l1 ? l1 : l2; | |
while (tmp) { | |
*tail = tmp; | |
tmp = tmp->next; | |
tail = &(*tail)->next; | |
} | |
return dummy.next; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment