-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeTwoLists.cpp
44 lines (37 loc) · 1.03 KB
/
mergeTwoLists.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/****************************************************************
Copyright : CNIC
Author : LiuYao
Date : 2017-8-30
Description : 21. Merge Two Sorted Lists
****************************************************************/
#include <cstddef>
//Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head = new ListNode(0), *cur = head;
//add l1 and l2 to the new linked list
while(l1 && l2){
if(l1 -> val < l2 -> val) {
cur -> next = l1;
l1 = l1 -> next;
}
else {
cur -> next = l2;
l2 = l2 -> next;
}
cur = cur -> next;
}
//cur->next points to l1 when l1 is not NULL, or points to l2.
cur -> next = l1 ? l1 : l2;
return head -> next;
}
int main(){
ListNode* l1 = new ListNode(2);
ListNode* l2 = new ListNode(1);
ListNode* res = mergeTwoLists(l1, l2);
return 0;
}