Merge Two Sorted Lists Solutions in C++
Number 21
Difficulty Easy
Acceptance 53.7%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/merge-two-sorted-lists/// Author : Hao Chen// Date : 2014-07-06/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public:Solution(){srand(time(NULL));}ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {switch (random()%3){case 0:return mergeTwoLists01(l1, l2);case 1:return mergeTwoLists02(l1, l2);default:return mergeTwoLists03(l1, l2);}}/* merge the 2nd list into 1st list*/ListNode *mergeTwoLists01(ListNode* head1, ListNode* head2){ListNode *p1 = head1, *p2=head2;static ListNode dummy(0);dummy.next = p1;ListNode *prev = &dummy;while(p1 && p2){if(p1->val < p2->val){prev = p1;p1 = p1->next;}else{prev->next = p2;p2 = p2->next;prev = prev->next;prev->next = p1;}}if (p2){prev->next = p2;}return dummy.next;}/* merge two lists to the new list */ListNode *mergeTwoLists02(ListNode *l1, ListNode *l2) {ListNode *l=NULL, *p=NULL;while (l1!=NULL && l2!=NULL ){ListNode *n=NULL;if (l1->val < l2-> val){n = l1;l1=l1->next;}else{n = l2;l2=l2->next;}if (l==NULL){l = p = n;}else{p->next = n;p = p->next;}}ListNode* rest = l1 ? l1 :l2;l = mergeTheRest(rest, l, p);return l;}ListNode* mergeTheRest(ListNode* l, ListNode*head, ListNode* tail){if (l){if (head && tail ){tail->next = l;}else{head = l;}}return head;}/** You can see the 2nd slution's code is quite complicated,* because it need to check the (head==NULL) situation.* We can use the "pointer to pointer" to make the code more clean* however, this would be bad for performance.*/ListNode *mergeTwoLists03(ListNode *l1, ListNode *l2) {ListNode *head = NULL;ListNode **pTail = &head;while (l1 != NULL && l2 != NULL) {if (l1->val < l2->val) {*pTail = l1;l1 = l1->next;} else {*pTail = l2;l2 = l2->next;}pTail = &(*pTail)->next;}*pTail = (l1 != NULL ? l1 : l2);return head;}};
C++ solution by pezy/LeetCode
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/#include <cstddef>struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {public:ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {ListNode *header = NULL, **p;for (p = &header; l1 && l2; p = &(*p)->next){if (l1->val <= l2->val){*p = new ListNode(l1->val);l1 = l1->next;}else{*p = new ListNode(l2->val);l2 = l2->next;}}for (ListNode *leave = l1 ? l1 : l2; leave; leave = leave->next, p = &(*p)->next)*p = new ListNode(leave->val);return header;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/merge-two-sorted-lists/description//// Author : liuyubobobo/// Time : 2017-12-01#include <iostream>using namespace std;/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};/// Iterative/// Time Complexity: O(len(l1) + len(l2))/// Space Complexity: O(1)class Solution {public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode* dummyHead = new ListNode(-1);ListNode* p = dummyHead;ListNode* l1p = l1;ListNode* l2p = l2;while(l1p != NULL && l2p != NULL){if(l1p->val < l2p->val){p->next = l1p;l1p = l1p->next;}else{p->next = l2p;l2p = l2p->next;}p = p->next;}if(l1p != NULL)p->next = l1p;elsep->next = l2p;ListNode* ret = dummyHead->next;dummyHead->next = NULL;delete dummyHead;return ret;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/merge-two-sorted-lists/description//// Author : liuyubobobo/// Time : 2017-12-01#include <iostream>using namespace std;/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};/// Recursive/// Time Complexity: O(len(l1) + len(l2))/// Space Complexity: O(len(l1) + len(l2))class Solution {public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {if(l1 == NULL)return l2;if(l2 == NULL)return l1;if(l1->val < l2->val){l1->next = mergeTwoLists(l1->next, l2);return l1;}l2->next = mergeTwoLists(l1, l2->next);return l2;}};int main() {return 0;}