Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Merge Two Sorted Lists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode* t1 = list1;
ListNode* t2 = list2;
ListNode* dummyNode = new ListNode(-1);
ListNode* temp = dummyNode;

while(t1 != NULL && t2 != NULL){
if(t1 -> val < t2 -> val){
temp->next = t1;
temp = t1;
t1 = t1->next;
}
else{
temp->next = t2;
temp = t2;
t2 = t2->next;

}
}
if(t1) temp->next =t1;
else{
temp->next = t2;
}
return dummyNode->next;
}
};
Loading