Language 晓彤 蒋 165. 合并两个排序链表 拼接,递归,新建
2018-03-24 13:18
411 查看
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
方法一:对原有链表进行拼接完成。太慢!!!!!!
class Solution {
public:
/**
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode * mergeTwoLists(ListNode * l1, ListNode * l2) {
// write your code here
if(l1 == NULL){
return l2;
}
if (l2 == NULL){
return l1;
}
if(l1->val > l2->val){
ListNode *temp = l1;
l1 = l2;
l2 = temp;
}
ListNode *head = l1;
ListNode *q = l2;
while (l2 != NULL && l1->next != NULL){
if (l1->val <= l2->val && l1->next->val >= l2->val){
q = q->next;
l2->next = l1->next;
l1->next = l2;
l1 = l1->next;
l2 = q;
}
else{
l1 = l1->next;
}
}
if (l2 != NULL){
l1->next = l2;
}
return head;
}
};
方法二:新建一个链表。少了1000ms,已经是三种方法里面最快的了!
class Solution {
public:
/**
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode * mergeTwoLists(ListNode * l1, ListNode * l2) {
// write your code here
ListNode *head = new ListNode(0);
ListNode *p = head;
while(l1 != NULL && l2 != NULL){
if(l1->val <= l2->val){
p->next = new ListNode(l1->val);
p = p->next;
l1 = l1->next;
}
else if(l1->val > l2->val){
p->next = new ListNode(l2->val);
p = p->next;
l2 = l2->next;
}
}
if (l1 == NULL){
p->next = l2;
}
if (l2 == NULL){
p->next = l1;
}
return head->next;
}
};
方法三:递归。阿勒阿勒,7000多ms,没想到更慢了!!!不过感觉方法很逼格!
class Solution {
public:
/**
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode * mergeTwoLists(ListNode * l1, ListNode * l2) {
// write your code here
ListNode *head = NULL;
if (l1 == NULL){
return l2;
}
if (l2 == NULL){
return l1;
}
if(l1->val <= l2->val){
head = l1;
l1->next = mergeTwoLists(l1->next, l2);
}
if(l1->val > l2->val){
head = l2;
l2->next = mergeTwoLists(l1, l2->next);
}
return head;
}
};
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
方法一:对原有链表进行拼接完成。太慢!!!!!!
class Solution {
public:
/**
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode * mergeTwoLists(ListNode * l1, ListNode * l2) {
// write your code here
if(l1 == NULL){
return l2;
}
if (l2 == NULL){
return l1;
}
if(l1->val > l2->val){
ListNode *temp = l1;
l1 = l2;
l2 = temp;
}
ListNode *head = l1;
ListNode *q = l2;
while (l2 != NULL && l1->next != NULL){
if (l1->val <= l2->val && l1->next->val >= l2->val){
q = q->next;
l2->next = l1->next;
l1->next = l2;
l1 = l1->next;
l2 = q;
}
else{
l1 = l1->next;
}
}
if (l2 != NULL){
l1->next = l2;
}
return head;
}
};
方法二:新建一个链表。少了1000ms,已经是三种方法里面最快的了!
class Solution {
public:
/**
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode * mergeTwoLists(ListNode * l1, ListNode * l2) {
// write your code here
ListNode *head = new ListNode(0);
ListNode *p = head;
while(l1 != NULL && l2 != NULL){
if(l1->val <= l2->val){
p->next = new ListNode(l1->val);
p = p->next;
l1 = l1->next;
}
else if(l1->val > l2->val){
p->next = new ListNode(l2->val);
p = p->next;
l2 = l2->next;
}
}
if (l1 == NULL){
p->next = l2;
}
if (l2 == NULL){
p->next = l1;
}
return head->next;
}
};
方法三:递归。阿勒阿勒,7000多ms,没想到更慢了!!!不过感觉方法很逼格!
class Solution {
public:
/**
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode * mergeTwoLists(ListNode * l1, ListNode * l2) {
// write your code here
ListNode *head = NULL;
if (l1 == NULL){
return l2;
}
if (l2 == NULL){
return l1;
}
if(l1->val <= l2->val){
head = l1;
l1->next = mergeTwoLists(l1->next, l2);
}
if(l1->val > l2->val){
head = l2;
l2->next = mergeTwoLists(l1, l2->next);
}
return head;
}
};
相关文章推荐
- 合并两个排序的链表(非递归)
- 合并两个已排序的链表(递归方法实现)
- 合并两个排序链表--迭代和递归分别实现
- 剑指Offer面试题16反转链表(递归和非递归),面试题17合并两个排序的链表(递归)
- 合并两个排序的链表(非递归)
- python_lintcode_93. 平衡二叉树_165. 合并两个排序链表_453. 将二叉树拆成链表
- 剑指offer面试题25:合并两个排序的链表(c++ 递归+非递归)
- 165. 合并两个排序链表
- lintcode 165. 合并两个排序链表
- 面试之路(29)-合并两个排序的链表(递归和非递归)
- 剑指Offer 面试题25:合并两个排序的链表(递归+非递归) Java代码实现
- 链表——(循环和递归)合并两个排序链表
- 面试之路(29)-合并两个排序的链表(递归和非递归)
- 面试之路(29)-合并两个排序的链表(递归和非递归)
- 面试之路(29)-合并两个排序的链表(递归和非递归)
- [LintCode]165.合并两个排序链表
- 165. 合并两个排序链表(merge-two-sorted-lists)(c++)----lintcode面试题之链表
- 【LintCode 简单】165. 合并两个排序链表
- 合并两个排序的链表---递归实现
- 剑指offer——面试题17:合并两个排序的链表(非递归方法和递归方法)