-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a781b4
commit 70e25af
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
solutions/21-Merge_Two_Sorted_Lists-合并两个有序链表/Merge Two Sorted Lists.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# python3 | ||
# simple | ||
# 链表 | ||
# 44ms 51.81% | ||
# 13.7MB 5.05% | ||
|
||
class Solution: | ||
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: | ||
l = ListNode(0) | ||
home = l | ||
while l1 != None and l2 != None: | ||
if l1.val < l2.val: | ||
l.next = ListNode(l1.val) | ||
l = l.next | ||
l1 = l1.next | ||
else: | ||
l.next = ListNode(l2.val) | ||
l = l.next | ||
l2 = l2.next | ||
if l1 != None: | ||
l.next = l1 | ||
if l2 != None: | ||
l.next = l2 | ||
return home.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# 21、合并两个有序链表 | ||
> tag: python 、 链表 | ||
*** | ||
### 题目描述 | ||
|
||
  将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 | ||
|
||
### 示例 | ||
|
||
``` | ||
输入:1->2->4, 1->3->4 | ||
输出:1->1->2->3->4->4 | ||
``` | ||
|
||
*** | ||
### 题目链接 | ||
[21. 合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/) | ||
|
||
*** | ||
### 题解 | ||
|
||
  很简单的一道题,只需要每次取 `l1, l2` 当前指向的值最小的一个加入到结果链表中,当 `l1, l2`中有一个到链表尾部时,将另一个没到链表尾部的剩余部分加入到结果中并返回。 | ||
|
||
```python | ||
class Solution: | ||
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: | ||
l = ListNode(0) | ||
home = l | ||
while l1 != None and l2 != None: | ||
if l1.val < l2.val: | ||
l.next = ListNode(l1.val) | ||
l = l.next | ||
l1 = l1.next | ||
else: | ||
l.next = ListNode(l2.val) | ||
l = l.next | ||
l2 = l2.next | ||
if l1 != None: | ||
l.next = l1 | ||
if l2 != None: | ||
l.next = l2 | ||
return home.next | ||
``` | ||
|
||
  最终结果,*运行时间44ms*,超过51.81%;*占用内存13.7MB*,超过5.05%。 |