19.1.29 [LeetCode 21] Merge Two Sorted Lists
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了19.1.29 [LeetCode 21] Merge Two Sorted Lists相关的知识,希望对你有一定的参考价值。
?????????nbsp event cli should sort amp splay ???????????? fir
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
1 class Solution { 2 public: 3 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 4 ListNode*head=new ListNode(0); 5 ListNode*ans = head; 6 while (l1&&l2) { 7 if (l1->val < l2->val) { 8 head->next = l1; 9 l1 = l1->next; 10 } 11 else { 12 head->next = l2; 13 l2 = l2->next; 14 } 15 head = head->next; 16 } 17 if (l1)head->next = l1; 18 else if (l2)head->next = l2; 19 return ans->next; 20 } 21 };
以上是关于19.1.29 [LeetCode 21] Merge Two Sorted Lists的主要内容,如果未能解决你的问题,请参考以下文章
19.1.29 [LeetCode 22] Generate Parentheses