您的位置:首页 > 其它

Merge Two Sorted Lists *****

2015-04-17 16:58 281 查看
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

***Note***

Why would it will cause ERROR when not deleting line 15~17 or changing line 12 to

ListNode *pre_head = null; or ListNode *pre_head;

You have to notice the definition of ListNode. When you understand that you can not create an object withour giving an argument, you won't make mistakes.

Actually, you don't need to delete line 15~17, just correct line 15 and you will even save time. After editing, the run time is 15ms while before is 16ms.

Newly version. Runtime: 8ms

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(!l1 && !l2) return l1;
if(!l1) return l2;
if(!l2) return l1;

ListNode* pre = new ListNode(0);
ListNode* move = pre;
while(l1 && l2){
ListNode* temp = new ListNode(0);
if(l1->val < l2->val){
temp->val = l1->val;
l1 = l1->next;
}
else{
temp->val = l2->val;
l2 = l2->next;
}
move->next = temp;
move = move->next;
}
if(l1)
move->next = l1;
if(l2)
move->next = l2;

return pre->next;
}
};


NEWLY VERSION!!!

Runtime: 8ms

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode pre(-1);

for(ListNode* p = ⪯ l1 || l2; p = p->next){
int val1 = l1 == nullptr ? INT_MAX : l1->val;
int val2 = l2 == nullptr ? INT_MAX : l2->val;

if(val1 > val2){
p->next = l2;
l2 = l2->next;
}
else{
p->next = l1;
l1 = l1->next;
}
}
return pre.next;
}
};


/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
//code below used 15ms on leetcode  platform
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if(!l1 && !l2) return NULL;
if(!l1) return l2;
if(!l2) return l1;

ListNode *result = new ListNode(0);
ListNode *pre_head = result;
while(l1 && l2){
if(l1->val <= l2->val){
result->next = l1;
l1 = l1->next;
}
else{
result->next = l2;
l2 = l2->next;
}
result = result->next;
}
while(l1){
result->next = l1;
result = result->next;
l1 = l1->next;
}
while(l2){
result->next = l2;
result = result->next;
l2 = l2->next;
}
return pre_head->next;
}
};


View Code

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode *pre_head = new ListNode(0);
ListNode *result = pre_head;

//if(!l1 && !l2) return result;
//if(!l1 && l2) return l2;
//if(l1 && !l2) return l1;

while(l1 || l2){
if(!l1){
while(l2){
result->next = l2;
result = result->next;
l2 = l2->next;
}
break;
}
else if(!l2){
while(l1){
result->next = l1;
result = result->next;
l1 = l1->next;
}
break;
}
else{
if(l1->val <= l2->val){
result->next = l1;
l1 = l1->next;
}
else{
result->next = l2;
l2 = l2->next;
}
}
result = result->next;
result->next = NULL;
}
return pre_head->next;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: