Skip to content

Commit

Permalink
[add] 21
Browse files Browse the repository at this point in the history
  • Loading branch information
D0ub1ePieR committed Mar 26, 2020
1 parent 2a781b4 commit 70e25af
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 21、合并两个有序链表
> tag: python 、 链表
***
### 题目描述

&emsp;&emsp;将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

### 示例

```
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
```

***
### 题目链接
[21. 合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/)

***
### 题解

&emsp;&emsp;很简单的一道题,只需要每次取 `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
```

&emsp;&emsp;最终结果,*运行时间44ms*,超过51.81%;*占用内存13.7MB*,超过5.05%。

0 comments on commit 70e25af

Please sign in to comment.