leetcode_效率题解_[python/C++]_21. Merge Two Sorted Lists(合并2个有序链表)
来源:互联网 发布:手机网络爸爸 编辑:程序博客网 时间:2024/04/30 07:41
相关题解:
leetcode_效率题解_23. Merge k Sorted Lists(合并k个有序链表)
题目链接
【题目】
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.
【分析】
解法1:
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(l1 == NULL) return l2; if(l2 == NULL) return l1; ListNode* new_list = NULL; if(l1->val < l2->val) { new_list = l1; l1 = l1->next; } else { new_list = l2; l2 = l2->next; } ListNode * pre = new_list; while( l1 != NULL && l2 != NULL ){ if( l1->val < l2->val ){ pre->next = l1; l1 = l1->next; } else{ pre->next = l2; l2 = l2->next; } pre = pre->next; } if( l1 ) pre->next = l1; else pre->next = l2; return new_list; }};
解法2:
前面加一个无关的节点,不用去比较两个链表第一个的大小
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(!l1 && !l2) return NULL; ListNode * new_list = new ListNode(0); ListNode * pre = new_list; while( l1 != NULL && l2 != NULL ){ if( l1->val < l2->val ){ pre->next = l1; l1 = l1->next; } else{ pre->next = l2; l2 = l2->next; } pre = pre->next; } if( l1 ) pre->next = l1; else pre->next = l2; return new_list->next; }};
解法3:
递归,leetcode上效率比较高
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; } else { l2->next = mergeTwoLists(l2->next, l1); return l2; } } };
python:
解法1:非递归:
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if l1 is None and l2 is None: return None new_list = ListNode(0) pre = new_list while l1 is not None and l2 is not None: if l1.val < l2.val: pre.next = l1 l1 = l1.next else: pre.next = l2 l2 = l2.next pre = pre.next if l1 is not None: pre.next = l1 else: pre.next = l2 return new_list.next
解法2:递归:
def mergeTwoLists(self, l1, l2): if not l1: return l2 elif not l2: return l1 else: if l1.val <= l2.val: l1.next = self.mergeTwoLists(l1.next, l2) return l1 else: l2.next = self.mergeTwoLists(l1, l2.next) return l2
解法3:
discuss上的一种解法,效率很高,但是我是不可能想到的
class Solution(object): def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ new_list = ListNode(0) new_list.next = l1 prev, head_of_1, head_of_2 = new_list, l1, l2 while head_of_2: if not head_of_1: prev.next = head_of_2 break if head_of_1.val > head_of_2.val: temp = head_of_2 head_of_2 = head_of_2.next prev.next = temp temp.next = head_of_1 prev = prev.next else: head_of_1, prev = head_of_1.next, head_of_1 return new_list.next
0 0
- leetcode_效率题解_[python/C++]_21. Merge Two Sorted Lists(合并2个有序链表)
- leetcode_效率题解_23. Merge k Sorted Lists(合并k个有序链表)
- Merge Two Sorted Lists(合并两个有序链表)
- 21. Merge Two Sorted Lists(合并有序链表)
- 两个有序链表的合并 Merge Two Sorted Lists
- Merge Two Sorted Lists 合并两个有序链表@LeetCode
- [LeetCode]Merge Two Sorted Lists-合并两个有序链表
- Merge Two Sorted Lists 合并两个有序链表
- Leetcode Merge Two Sorted Lists 合并两个有序链表
- Merge Two Sorted Lists, 合并两个有序链表
- leetcode Merge Two Sorted Lists 合并两个有序链表
- Leetcode Merge Two Sorted Lists(合并两个有序表)
- LeetCode--Merge Two Sorted Lists(合并两个有序链表)Python
- 合并k个有序链表 Merge k Sorted Lists
- Merge k Sorted Lists(合并k个有序链)
- 每日算法之四十五:Merge Two Sorted Lists(合并有序链表)
- LeetCode OJ 之 Merge Two Sorted Lists(合并两个有序的链表)
- [LeetCode-21] Merge Two Sorted Lists(合并两个有序链表)
- 数据库的升级原理详解
- 线上幽灵 - 笔记
- C#学习笔记1
- 图解aclocal、autoconf、automake、autoheader、configure
- 初识Android MVP(笔记)
- leetcode_效率题解_[python/C++]_21. Merge Two Sorted Lists(合并2个有序链表)
- bash: /bin/rm: 参数列表过长
- Tomcat配置及使用
- Android 中 activity 的生命周期和数据储存
- HTTP 状态吗详解
- 指针
- Java基础——反射
- Hive连接与编程
- 第十二周项目四汉诺塔