1.合并两个有序链表
题目链接:https://leetcode.cn/problems/merge-two-sorted-lists/
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()
curr = dummy
while list1 and list2:
if list1.val < list2.val:
curr.next = list1
list1 = list1.next
else:
curr.next = list2
list2 = list2.next
curr = curr.next
curr.next = list1 if list1 else list2
return dummy.next
2. 括号生成
题目链接:https://leetcode.cn/problems/generate-parentheses/
数字 n 代表生成括号的对数,请设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
result = []
def backtrack(left, right, current):
if len(current) == 2 * n:
result.append(current)
return
if left < n:
backtrack(left + 1, right, current + '(')
if right < left:
backtrack(left, right + 1, current + ')')
backtrack(0, 0, '')
return result
标签:current,curr,Study,Part11,list1,next,链表,Algorithms,list2
From: https://www.cnblogs.com/stephenxiong001/p/18378664