-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.Merge Two Sorted Lists.cpp
44 lines (39 loc) · 1.07 KB
/
21.Merge Two Sorted Lists.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// Two pointer points to two list, find the smaller one to add in the new List
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* p1 = l1, *p2 = l2;
ListNode* pPre = NULL;
if(l1 == NULL)
return l2;
while(p1!=NULL&&p2!=NULL){
if(p1->val<p2->val){
pPre = p1;
p1 = p1->next;
}else{
if(pPre != NULL){
pPre->next = p2;
p2=p2->next;
pPre->next->next = p1;
pPre = pPre->next;
}else{
l1 = p2;
p2=p2->next;
pPre = l1;
pPre->next = p1;
}
}
}
if(p2!= NULL)
pPre->next = p2;
return l1;
}
};