【力扣hot100】刷题笔记Day9
前言
- 阴天睡得还挺舒服,9点半才醒,用刷题开启美好新一天!
141. 环形链表 - 力扣(LeetCode)
142. 环形链表 II - 力扣(LeetCode)
-
快慢指针
-
class Solution:def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:fast = slow = headwhile fast and fast.next:fast = fast.next.nextslow = slow.nextif slow == fast: # 快慢指针相遇,s走了nb,f走了2nbfast = head # 入口处需要a+nb,s已经走了nbwhile fast != slow: # 同时走a就可到达入口fast = fast.nextslow = slow.nextreturn fastreturn None
21. 合并两个有序链表 - 力扣(LeetCode)
-
迭代法
-
class Solution:def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:prehead = ListNode(-1)precur = preheadwhile list1 and list2:if list1.val <= list2.val: # 把较小值接到precur后面precur.next = list1list1 = list1.nextelse:precur.next = list2list2 = list2.nextprecur = precur.next # 记得让precur前进# 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可precur.next = list1 if list1 else list2return prehead.next
-
递归法
-
class Solution:def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:if list1 is None:return list2if list2 is None:return list1if list1.val < list2.val:list1.next = self.mergeTwoLists(list1.next, list2)return list1else:list2.next = self.mergeTwoLists(list2.next, list1)return list2
2. 两数相加 - 力扣(LeetCode)
-
模拟——转数字
-
class Solution:def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:# 链表转化为数字def getNum(head):num = 0flag = 1while head != None:num += head.val * flaghead = head.nextflag *= 10return num# 计算和 twoSum = getNum(l1) + getNum(l2)# 和为0直接返回0节点if twoSum == 0:return ListNode(0)# 循环生成新链表head = cur = ListNode(-1)while twoSum != 0:cur.next = ListNode(twoSum % 10)cur = cur.nexttwoSum //= 10 # 整除return head.next
-
模拟——进位
-
class Solution:def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:# 创建一个结点值为 None 的头结点, dummy 和 p 指向头结点, dummy 用来最后返回, p 用来遍历dummy = p = ListNode(None) s = 0 # 初始化进位 s 为 0while l1 or l2 or s:# 如果 l1 或 l2 存在, 则取l1的值 + l2的值 + s(s初始为0, 如果下面有进位1, 下次加上)s += (l1.val if l1 else 0) + (l2.val if l2 else 0) p.next = ListNode(s % 10) # p.next 指向新链表, 用来创建一个新的链表p = p.next # p 向后遍历s //= 10 # 有进位情况则取模, eg. s = 18, 18 // 10 = 1l1 = l1.next if l1 else None # 如果l1存在, 则向后遍历, 否则为 Nonel2 = l2.next if l2 else None # 如果l2存在, 则向后遍历, 否则为 Nonereturn dummy.next # 返回 dummy 的下一个节点, 因为 dummy 指向的是空的头结点, 下一个节点才是新建链表的后序节点
-
递归——进位
-
class Solution:def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:def dfs(l, r, i):# 终止条件:都为空节点且进位为0if not l and not r and not i: return Nones = (l.val if l else 0) + (r.val if r else 0) + inode = ListNode(s % 10) # 当前节点node.next = dfs(l.next if l else None, r.next if r else None, s // 10) # 接上递归链表return nodereturn dfs(l1, l2, 0)
19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
-
双指针
-
class Solution:def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:dummy = ListNode(0, head) # 直接创建虚拟头节点fast = slow = dummy # 双指针# 快指针先走while n != 0:fast = fast.nextn -= 1# 双指针一起走直到fast到底while fast.next != None:fast = fast.nextslow = slow.next# 删除节点slow.next = slow.next.nextreturn dummy.next
24. 两两交换链表中的节点 - 力扣(LeetCode)
-
模拟
-
class Solution:def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:dummy = ListNode(0, head)cur = dummywhile cur.next and cur.next.next:pre = curcur = cur.nexttemp = cur.next.nextpre.next = cur.next # 步骤1pre.next.next = cur # 步骤2cur.next = temp # 步骤3return dummy.next
-
递归
后言
- 明天估计是不开组会的,今天猛猛刷了6道题!知识能进脑子的感觉真爽啊!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/484721.html
如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!