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
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 | # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution( object ): def mergeTwoLists( self , l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ dummy = cur = ListNode( None ) while l1 and l2: if l1.val < = l2.val: cur. next = ListNode(l1.val) l1 = l1. next else : cur. next = ListNode(l2.val) l2 = l2. next cur = cur. next l = l1 or l2 while l: cur. next = ListNode(l.val) l = l. next cur = cur. next return dummy. next |
可以稍微少几行代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution( object ): def mergeTwoLists( self , l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ dummy = cur = ListNode( None ) while l1 or l2: if (l1 and l2 and l1.val < = l2.val) or l2 is None : cur. next = ListNode(l1.val) l1 = l1. next elif (l1 and l2 and l1.val > l2.val) or l1 is None : cur. next = ListNode(l2.val) l2 = l2. next cur = cur. next return dummy. next |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution( object ): def mergeTwoLists( self , l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if (l1 and l2 and l1.val < = l2.val) or (l1 and not l2): node = ListNode(l1.val) node. next = self .mergeTwoLists(l1. next , l2) return node elif (l1 and l2 and l1.val > l2.val) or (l2 and not l1): node = ListNode(l2.val) node. next = self .mergeTwoLists(l1, l2. next ) return node else : return None |
还有偷懒的解法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Solution { public: ListNode * mergeTwoLists(ListNode * l1, ListNode * l2) { ListNode dummy(INT_MIN); ListNode * tail = &dummy; while (l1 && l2) { if (l1 - >val < l2 - >val) { tail - > next = l1; l1 = l1 - > next ; } else { tail - > next = l2; l2 = l2 - > next ; } tail = tail - > next ; } tail - > next = l1 ? l1 : l2; return dummy. next ; } }; |
看到其他人的解法,如果允许修改原有list的话,还是可以的:
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 | # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution( object ): def mergeTwoLists( self , l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ dummy = cur = ListNode( None ) while l1 and l2: if l1.val < l2.val: cur. next = l1 l1 = l1. next else : cur. next = l2 l2 = l2. next cur = cur. next cur. next = l1 or l2 return dummy. next |
对应的递归解法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution( object ): def mergeTwoLists( self , l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if l1 is None : return l2 if l2 is None : 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 |
标签:
leetcode
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2017-04-14 echo 到 stderr