Skip to content

Commit cd61f4c

Browse files
author
xiapengchng
committed
Update solution 021 with cpp version
1 parent 62ddf67 commit cd61f4c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
12+
ListNode *a1=new ListNode(0);
13+
ListNode *head=a1;
14+
while(l1!=NULL&&l2!=NULL)
15+
{
16+
if(l1->val<l2->val)
17+
{
18+
a1->next=l1;
19+
l1=l1->next;
20+
a1=a1->next;
21+
}
22+
else
23+
{
24+
a1->next=l2;
25+
l2=l2->next;
26+
a1=a1->next;
27+
}
28+
}
29+
if(l1==NULL)
30+
a1->next=l2;
31+
if(l2==NULL)
32+
a1->next=l1;
33+
return head->next;
34+
35+
36+
}
37+
};

0 commit comments

Comments
 (0)