[LeetCode] 21. Merge Two Sorted Lists 合并有序链表
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.
Example:
Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4
和88. Merge Sorted Array类似,数据结构不一样,这里是合并链表。
由于是链表,不能像数组一样有从后面往前写的技巧。
解法1:dummy list,新建一个链表,然后两个链表中从头各取一个元素进行比较,小的写入新链表,直到结束,返回dummy.next。
解法2:recursion,代码简洁,但空间复杂度高O(n)
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class ListNode { int val; ListNode next; ListNode( int x) { val = x; } } public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode flag = new ListNode( 0 ); ListNode firstflag = flag; while (l1 != null && l2 != null ) { if (l1.val < l2.val){ flag.next = l1; l1 = l1.next; } else { flag.next = l2; l2 = l2.next; } flag = flag.next; } flag.next = l1 != null ? l1 : l2; return firstflag.next; } } |
Java:
1 2 3 4 5 6 7 8 9 10 11 | 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(l1, l2.next); return l2; } } |
Python: Time: O(n), Space: O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class ListNode( object ): def __init__( self , x): self .val = x self . next = None def __repr__( self ): if self : return "{} -> {}" . format ( self .val, self . next ) class Solution( object ): def mergeTwoLists( self , l1, l2): curr = dummy = ListNode( 0 ) while l1 and l2: if l1.val < l2.val: curr. next = l1 l1 = l1. next else : curr. next = l2 l2 = l2. next curr = curr. next curr. next = l1 or l2 return dummy. next |
Python: wo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class Solution( object ): def mergeTwoLists( self , l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head = ListNode( 0 ) dummy = head while l1 or l2: if l1 and l2: if l1.val < l2.val: dummy. next = l1 dummy = dummy. next # required l1 = l1. next else : dummy. next = l2 dummy = dummy. next # required l2 = l2. next elif l1: dummy. next = l1 break elif l2: dummy. next = l2 break return head. next |
Python: Recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Solution( object ): def mergeLists(head1, head2): temp = None if head1 is None : return head2 if head2 is None : return head1 if head1.val < = head2.val: temp = head1 temp. next = mergeLists(head1. next , head2) else : temp = head2 temp. next = mergeLists(head1, head2. next ) return temp |
Python: Recursive, wo, Time: O(n), Space: O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Solution( object ): def mergeTwoLists( self , l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if not l1: return l2 if not l2: return l1 if l1.val < l2.val: l1. next = self .mergeTwoLists(l1. next , l2) return l1 else : l2. next = self .mergeTwoLists(l1, l2. next ) return l2 |
Python: in-place, iteratively
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | def mergeTwoLists( self , l1, l2): if None in (l1, l2): return l1 or l2 dummy = cur = ListNode( 0 ) dummy. next = l1 while l1 and l2: if l1.val < l2.val: l1 = l1. next else : nxt = cur. next cur. next = l2 tmp = l2. next l2. next = nxt l2 = tmp cur = cur. next cur. next = l1 or l2 return dummy. next |
C++: Time: O(n), Space: O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | /** * 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 dummy{0}; auto curr = &dummy; while (l1 && l2) { if (l1->val <= l2->val) { curr->next = l1; l1 = l1->next; } else { curr->next = l2; l2 = l2->next; } curr = curr->next; } curr->next = l1 ? l1 : l2; return dummy.next; } }; |
C++: Recursive
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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; } } }; |
类似题目:
[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
[LeetCode] 88. Merge Sorted Array 合并有序数组
All LeetCode Questions List 题目汇总
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架