-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAY 10 MERGE TWO SORTED ARRAY.cpp
49 lines (42 loc) · 1.04 KB
/
DAY 10 MERGE TWO SORTED ARRAY.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
45
46
47
48
49
// MERGE TWO SORTED ARRAY.CPP LEETCODE PROBLEM
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {// l1 list l1 and l2 list l2
if(l1==NULL)
{ return l2;
}if(l2==NULL)
{ return l1;
}
if(l1-> val <= l2->val )
{
l1->next=mergeTwoLists(l1->next,l2);
return l1;
}
else
{
l2->next=mergeTwoLists(l2->next,l1);
return l2;
}
}
};
// REVERSE LINKED LIST.cpp LEETCODE SOLUTION C++ , CPP PROBLEM2
class Solution {
public:
ListNode* reverseList(ListNode* head) {
// Recursive approach
if(!head || !(head->next)) return head;
auto res = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return res;
}
};
// Second approach for itrative
/* ListNode *prev = NULL, *cur=head, *tmp;
while(cur){
tmp = cur->next;
cur->next = prev;
prev = cur;
cur = tmp;
}
return prev;*/