-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathMergeTwoSortedLinkedList.cpp
49 lines (46 loc) · 1.29 KB
/
MergeTwoSortedLinkedList.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
// This is the solution for Merge Two Sorted LinkedList question from Leetcode
// https://leetcode.com/problems/merge-two-sorted-lists/description/
/**
* Definition for singly-linked list.
* 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) {}
* };
*/
class Solution {
public:
void change(ListNode* &prev, ListNode* &curr, ListNode* &newHead){
if(prev==NULL){
newHead= curr;
prev= curr;
curr= curr->next;
}
else{
prev->next= curr;
prev= curr;
curr= curr->next;
}
prev->next= NULL;
}
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode* prev= NULL;
ListNode* newHead= NULL;
while(list1!=NULL && list2!=NULL){
if(list1->val<list2->val){
change(prev, list1, newHead);
}else{
change(prev, list2, newHead);
}
}
while(list1!=NULL){
change(prev, list1, newHead);
}
while(list2!=NULL){
change(prev, list2, newHead);
}
return newHead;
}
};