pta两个有序链表的合并_LeetCode21.合并两个有序链表(Merge Two Sorted Lists)

12e164dfbbe9f4e00693b7d2ea83660a.png

21. Merge Two Sorted Lists

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

示例:

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

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/

Link:https://leetcode.com/problems/merge-two-sorted-lists/

双指针

O(N)

比较两个链表头,把小的拿出来放到新的链表中

# Definition for singly-linked list.# class ListNode:#     def __init__(self, val=0, next=None):#         self.val = val#         self.next = nextclass Solution:    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:        cur = head = ListNode()        while(l1 is not None and l2 is not None):            if l1.val <= l2.val:                cur.next = l1                l1 = l1.next            else:                cur.next = l2                l2 = l2.next            cur = cur.next        cur.next = l1 if l1 is not None else l2        return head.next

可以将链表想像成无穷多个,结尾以无穷大结束

# Definition for singly-linked list.# class ListNode:#     def __init__(self, val=0, next=None):#         self.val = val#         self.next = nextclass Solution:    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:        cur = head = ListNode()        while(l1 is not None or l2 is not None):            l1_val = l1.val if l1 is not None else float('inf')             l2_val = l2.val if l2 is not None else float('inf')             if l1_val <= l2_val:                cur.next = l1                l1 = l1.next            else:                cur.next = l2                l2 = l2.next            cur = cur.next        return head.next

17. 电话号码的字母组合(Letter Combinations of a Phone Number)

感谢知乎用户(@一个小老虎)的评论反馈

递归有栈溢出的风险, 如果不用递归, 那该怎么实现?

排列组合还可以用队列Queue实现,而且还能够处理动态输入的情况

以'23'为例, 初始化队列包含空字符

['']

先输入2, 将2对应的字母'abc'分别拼接上队列中长度等于0的, 也就是空字符''

['a', 'b', 'c']

在输入3, 将3对应的字母'def'分别拼接上队列中长度等于1的, 也就是字母'a','b'和'c'

['b', 'c', 'ad', 'ae', 'af']['c', 'ad', 'ae', 'af', 'bd', 'be', 'bf']['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']

代码如下:

from collections import dequeclass Solution:        def letterCombinations(self, digits: str) -> List[str]:            if len(digits) == 0:            return []        digitToLetter = {            '2': 'abc',            '3': 'def',            '4': 'ghi',            '5': 'jkl',            '6': 'mno',            '7': 'pqrs',            '8': 'tuv',            '9': 'wxyz',        }        ans = deque()        ans.append('')        for i in range(len(digits)):            num = digits[i]            while len(ans) > 0 and len(ans[0]) == i:                 prefix = ans.popleft()                for letter in digitToLetter[num]:                    ans.append(prefix + letter)        return list(ans)

--End--

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值