Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2047347

Browse files
committed
add 021
1 parent 3ef1951 commit 2047347

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎LeetCodeSolutions/021.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#==================================================
2+
#==> Title: 21. 合并两个有序链表
3+
#==> Author: Zhang zhen
4+
#==> Email: hustmatnoble.gmail.com
5+
#==> GitHub: https://github.com/MatNoble
6+
#==> Date: 2/7/2021
7+
#==================================================
8+
9+
"""
10+
https://leetcode-cn.com/problems/merge-two-sorted-lists/
11+
"""
12+
13+
# Definition for singly-linked list.
14+
# class ListNode:
15+
# def __init__(self, val=0, next=None):
16+
# self.val = val
17+
# self.next = next
18+
class Solution:
19+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
20+
res = ListNode()
21+
cur = res
22+
while l1 and l2:
23+
if l1.val < l2.val:
24+
cur.next = l1
25+
l1 = l1.next
26+
else:
27+
cur.next = l2
28+
l2 = l2.next
29+
cur = cur.next
30+
cur.next = l1 if l1 else l2
31+
return res.next
32+
33+
"""
34+
时间复杂度: O(max(m, n))
35+
空间复杂度: O(1)
36+
"""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /