反转链表(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. 支付宝支付功能接入(PC)

    在使用支付宝支付功能开发的阶段, 可以在沙箱环境下进行开发, 开发完成之后, 到线上再进行相关参数的替换即可 一. 登陆支付宝开放平台(https://open.alipay.com/platform ...

  2. 序列化-serialVersionUID作用

    Serializable接口 作用:标记一个类可以被序列化,如果没有实现该接口,则会抛出异常. ObjectOutputStream中源码: 实验: serialVersionUID 作用:表示一个序 ...

  3. 【Amadeus原创】word图片隐藏在文字里了的终极解决办法

    终极解决方案: 点击该图片,然后,选择正文,即可.

  4. GraphQL Part IV: 浏览器内的 IDE

    只是一个使用,这里不做介绍了.

  5. django介绍及基本使用

    目录 一.python主流web框架 二.django简介 1.版本问题 2.运行django注意事项 三.django基本使用 1.下载模块 2.验证 3.常见命令 4.pycharm自动创建dja ...

  6. 【Python】【Flask】【字符串索引】计算人民币与美元的相互计算

    目录 简介 Python Code 导包 设置首页 计算的接口 问题0:设置请求方式 问题1:关于接收数据可能存在的问题 问题2:返回结果 启动 完整代码 HTML Code 问题分析 分析:获取下拉 ...

  7. 【前端】【探究】HTML - input类型为file时如何实现自定义文本以更好的美化

    想到英语四级考了两次都没过,我觉得要多使用英文,所以本文使用英文书写. 本文讲述了遇到的问题,解决的思路,并讲述了解决方案,也许对你会有帮助. 目录 Problem description Solut ...

  8. 【报错解决】【Vue】与后端交互时,http与https跨域问题

    问题 xhr.js:220 Mixed Content: The page at 'https://xxx' was loaded over HTTPS, but requested an insec ...

  9. 关于在VMware中安装的CentOS7系统中无法安装ntp

    一.问题引言 今天在虚拟机中新安装了CentOS7,在使用yum命令时,出现如下如错误: 2.点击图片中链接,即是"2"中的红框,发现该链接竟不可达 3.于是开始找度娘,但并没有发 ...

  10. Qt编写物联网管理平台31-用户权限管理

    一.前言 随着需求的不断变化,功能的增多,在用户信息这块,除了需要用户登录退出验证以外,还需要有个简单的用户权限逻辑处理,比如限定某些用户只有查看权限,没有删除记录.清空记录.系统设置的权限,与之相对 ...