Skip to content

Instantly share code, notes, and snippets.

@sonald
Last active August 29, 2015 14:08
Show Gist options
  • Save sonald/367efbdc0becee379b81 to your computer and use it in GitHub Desktop.
Save sonald/367efbdc0becee379b81 to your computer and use it in GitHub Desktop.
leetcode: merge tow sorted lists
// 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