-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSortedLists.cpp
81 lines (69 loc) · 2.86 KB
/
mergeSortedLists.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
// ListNode list1e3(4);
// ListNode list1e2(2, &list1e3);
// ListNode list1e1(1, &list1e2);
// ListNode *tempptr1=&list1e1;
// ListNode list2e3(4);
// ListNode list2e2(3, &list2e3);
// ListNode list2e1(1, &list2e2);
// ListNode *tempptr2=&list2e1;
class Solution {
public:
void createnode(int i, ListNode* ¤t3, ListNode* &answer)
{
if(i==0)
{
current3=new ListNode();
answer=current3;
}else{
(current3->next)=new ListNode();
current3=current3->next;
}
}
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode *current1=list1, *current2=list2;//, *current3=&list3;
ListNode *answer=nullptr;// = new ListNode();
ListNode *current3;//=answer;
for(int i=0; (current1!=nullptr || current2!=nullptr); i++)
{
if(current1==nullptr)
{
createnode(i, current3, answer);
current3->val=current2->val;//add list1 value to the newly created node
current2=current2->next;//current1 now points to the next value in list1
}else if(current2==nullptr)
{
createnode(i, current3, answer);
current3->val=current1->val;//add list1 value to the newly created node
current1=current1->next;//current1 now points to the next value in list1
}else if(current1->val<current2->val)
{
createnode(i, current3, answer);
current3->val=current1->val;//add list1 value to the newly created node
current1=current1->next;//current1 now points to the next value in list1
}else if(current2->val<current1->val)
{
createnode(i, current3, answer);
current3->val=current2->val;//add list1 value to the newly created node
current2=current2->next;//current1 now points to the next value in list1
}else if(current1->val==current2->val)
{
createnode(i, current3, answer);
current3->val=current1->val;//add list1 value to the newly created node
current1=current1->next;//current1 now points to the next value in list1
(current3->next)=new ListNode();
current3=current3->next;
current3->val=current2->val;//add list1 value to the newly created node
current2=current2->next;//current1 now points to the next value in list1
}
}
return answer;
}
};