反转链表(leetcode 206)

这个题目我就比较流氓了,干脆新建链表翻转过来算了。但是完蛋,超出内存限制,那我就只能两两换了。这里比较大的技巧就是可以用一个空节点进行置换。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
p,q=head,None
while p:
p.next,q,p=q,p,p.next
return q

反转链表II(leetcode 092)

这个还是一样的思想,用空节点的方法解决这个问题。

class Solution:
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
p0 = dummy = ListNode(next=head)
for _ in range(left - 1):
p0 = p0.next pre = None
cur = p0.next
for _ in range(right - left + 1):
nxt = cur.next
cur.next = pre # 每次循环只修改一个 next,方便大家理解
pre = cur
cur = nxt # 见视频
p0.next.next = cur
p0.next = pre
return dummy.next

K个一组反转链表(leetcode 025)

这个题号我怎么感觉这么乱呢,难度递增题号递减是吧

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
n = 0
cur = head
while cur:
n += 1 # 统计节点个数
cur = cur.next p0 = dummy = ListNode(next=head)
pre = None
cur = head
while n >= k:
n -= k
for _ in range(k): # 同 92 题
nxt = cur.next
cur.next = pre # 每次循环只修改一个 next,方便大家理解
pre = cur
cur = nxt # 见视频
nxt = p0.next
nxt.next = cur
p0.next = pre
p0 = nxt
return dummy.next

回文链表(leetcode 234)

这个题我终于可以使用流氓方法了哈哈哈哈

class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
s=[]
while head.next:
s.append(head.val)
head=head.next
s.append(head.val)
return s==s[::-1]

合并两个有序链表(leetcode 021)

这个题本质是一个归并排序,遵循数据结构里面的,先排,然后谁剩下就把谁拼最后。

class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
p=ListNode()
newhead=ListNode(next=p)
if list1==None and list2==None:
return None
if list1==None:
return list2
if list2==None:
return list1
while list1 and list2:
if list1.val>list2.val:
p.next=list2
list2=list2.next
else:
p.next=list1
list1=list1.next
p=p.next
if list1:
p.next=list1
else:
p.next=list2
return newhead.next.next

排序链表(leetcode 148)

这个题比较简单,跟上面的方法一样

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
s=[]
p=ListNode(next=head)
q=ListNode(next=p)
while head:
s.append(head.val)
head=head.next
s.sort()
p=p.next
for i in s:
p.val=i
p=p.next
return q.next.next

合并K个有序链表(leetcode 023)

这个题我们参考21题的代码进行融合就可以了。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
# 21. 合并两个有序链表
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
cur = dummy = ListNode() # 用哨兵节点简化代码逻辑
while list1 and list2:
if list1.val < list2.val:
cur.next = list1 # 把 list1 加到新链表中
list1 = list1.next
else: # 注:相等的情况加哪个节点都是可以的
cur.next = list2 # 把 list2 加到新链表中
list2 = list2.next
cur = cur.next
cur.next = list1 if list1 else list2 # 拼接剩余链表
return dummy.next def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
m = len(lists)
if m == 0: return None # 注意输入的 lists 可能是空的
if m == 1: return lists[0] # 无需合并,直接返回
left = self.mergeKLists(lists[:m // 2]) # 合并左半部分
right = self.mergeKLists(lists[m // 2:]) # 合并右半部分
return self.mergeTwoLists(left, right) # 最后把左半和右半合并

环形链表(leetcode 141)

这个题目昨天面试的时候被问到过,马上就想到了快慢指针法。昨天刚手搓过一回

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
p=ListNode(next=head)
q=ListNode(next=head)
while p and q:
p=p.next
q=q.next.next
if p==q:
return True
return False

环形链表II(leetcode 142)

这个解法是昨天面试搓的另一个方法。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def detectCycle(self, head: Optional[ListNode]) -> bool:
visited = set()
temp = head
while temp:
if temp in visited:
return temp
visited.add(temp)
temp = temp.next
return None

相交链表(leetcode 160)

这道题是以往的一道考研原题,以前复习考研的时候做过。这道题应该是408在2011年的一道题目改编,当时是以字母的形式考察相交链表。本质上就是先移动长表,然后一起走。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
p1=ListNode(next=headA)
q1=ListNode(next=headB)
p=ListNode(next=headA)
q=ListNode(next=headB)
len1,len2=0,0
while p1:
len1+=1
p1=p1.next
while q1:
len2+=1
q1=q1.next
if len1>len2:
for i in range(len1-len2):
p=p.next
if len2>len1:
for i in range(len2-len1):
q=q.next
for i in range(min(len1,len2)-1):
if p.next==q.next:
return p.next
p=p.next
q=q.next
return None

删除链表的倒数第N个节点(leetcode 019)

疯狂try&except去调试最后跑过了几个案例。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
p=ListNode(next=head)
q=ListNode(next=head)
h=ListNode(next=head)
count=0
for i in range(n):
try:
q=q.next
count+=1
except:
return None
try:
while q.next:
p=p.next
q=q.next
count+=1
except:
pass
print(count)
if count==n:
return None
p.next=p.next.next
return h.next

重排链表(leetcode 143)

解法仍然是快慢指针法。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
fast = slow = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
cur = slow.next
slow.next = None
pre = None
while cur:
t = cur.next
cur.next = pre
pre, cur = cur, t
cur = head
while pre:
t = pre.next
pre.next = cur.next
cur.next = pre
cur, pre = pre.next, t

datawhale-leetcode打卡:第026~037题的更多相关文章

  1. leetcode打卡

    leetcode刷题打卡 刷题链接 夸夸群 刷题记录链接 期中颜色不一样的,是刷题中遇到问题的,以后需要强化 [x] 6.1 打卡 [x] 6.2 打卡 中间因个人原因没做题,后面慢慢补上 [x] 6 ...

  2. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  3. 【LeetCode】树(共94题)

    [94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...

  4. LeetCode (85): Maximal Rectangle [含84题分析]

    链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...

  5. HDU 2546 饭卡(01背包裸题)

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  6. 【LeetCode】BFS(共43题)

    [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...

  7. 【LeetCode】Recursion(共11题)

    链接:https://leetcode.com/tag/recursion/ 247 Strobogrammatic Number II (2019年2月22日,谷歌tag) 给了一个 n,给出长度为 ...

  8. 【LeetCode动态规划#11】打家劫舍系列题(涉及环结构和树形DP的讨论)

    打家劫舍 力扣题目链接(opens new window) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻 ...

  9. 【一天一道LeetCode】#100. Same Tree(100题大关)

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  10. LeetCode 136. Single Number C++ 结题报告

    136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...

随机推荐

  1. Blazor 组件库 BootstrapBlazor 中Alert组件介绍

    组件介绍 Alert组件几乎是组件库里必不可少的组件了,即使浏览器,也自带了一个alert,会弹出一个模态框. 但是这个模态框有点太丑了,所以各大组件库分分实现了自己的Alert组件. 当然Boots ...

  2. Vue.js vuex

    1.前言 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式,它解决了vue中不同组件之间状态共享的问题. 通俗的说,它就是一个带响应式的全局变量管理,它数据的改变会触发相关页面/组件的更 ...

  3. RAG七十二式:2024年度RAG清单

    回顾2024,大模型日新月异,智能体百家争鸣.作为AI应用的重要组成部分,RAG也是"群雄逐鹿,诸侯并起".年初ModularRAG持续升温.GraphRAG大放异彩,年中开源工具 ...

  4. element 表格增加多选框 ,增加多选

    1. el-table 增加如下代码,就会出现多选框 <el-table-column type="selection" class="checkbox-inp&q ...

  5. 使用 .NET 的 Dev Proxy 构建和测试弹性应用

    使用 .NET 的 Dev Proxy 构建和测试弹性应用 https://devblogs.microsoft.com/dotnet/build-test-resilient-apps-dotnet ...

  6. 06 IdentityServer4 中支持外部标识提供器

    06. IdentityServer4 External Providers .NET Core 3.1 January 24, 2020| deblokt| in category Tutorial ...

  7. Spring Boot 2.4 中文

    Spring Boot 2.4 中文 https://runebook.dev/zh-CN/docs/spring_boot/spring-boot-features

  8. Qt编写地图综合应用48-地球模式、三维模式、地铁模式

    一.前言 百度地图本身提供了普通模式.地球模式.三维模式.地铁模式等好多种,普通模式是最常用的默认的,就是那个街道图和卫星图的,而地球模式和三维模式是最近几年才新增加的,为了迎合现在越来越多的用户的需 ...

  9. Qt编写地图综合应用16-省市轮廓图下载

    一.前言 之前做获取边界点的时候,主要采用的是在线地图的方式,因为在线地图中直接内置了函数可以根据行政区域的名称来自动获取边界,其实这些边界就是一些点坐标集合连接起来的平滑线,然后形成的轮廓图,这种方 ...

  10. GitHub Workflow 和 Action 的一些注意事项

    GitHub 的 workflow 和 action 存在一些注意事项,总结如下,以供参考 Workflow on.issues.types 如果需要判断 label,不需要指定 opened,只需要 ...