当前位置: 首页 > wzjs >正文

html可以做网站吗怎样宣传一个网站

html可以做网站吗,怎样宣传一个网站,完成公司网站建设,株洲网站建设哪家好链表 基本技能 链表相关的核心点 null/nil 异常处理dummy node 哑巴节点快慢指针插入一个节点到排序链表从一个链表中移除一个节点翻转链表合并两个链表找到链表的中间节点 常见题型 remove-duplicates-from-sorted-list 给定一个排序链表,删除所有重复的元素&…

链表

基本技能

链表相关的核心点

  • null/nil 异常处理
  • dummy node 哑巴节点
  • 快慢指针
  • 插入一个节点到排序链表
  • 从一个链表中移除一个节点
  • 翻转链表
  • 合并两个链表
  • 找到链表的中间节点

常见题型

remove-duplicates-from-sorted-list

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

class Solution:def deleteDuplicates(self, head: ListNode) -> ListNode:if head is None:return headcurrent = headwhile current.next is not None:if current.next.val == current.val:current.next = current.next.nextelse:current = current.nextreturn head

remove-duplicates-from-sorted-list-ii

给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现的数字。

  • 思路:链表头结点可能被删除,所以用 dummy node 辅助删除
class Solution:def deleteDuplicates(self, head: ListNode) -> ListNode:if head is None:return headdummy = ListNode(next=head)current, peek = dummy, headfind_dup = Falsewhile peek.next is not None:if peek.next.val == peek.val:find_dup = Truepeek.next = peek.next.nextelse:if find_dup:find_dup = Falsecurrent.next = current.next.nextelse:current = current.nextpeek = peek.nextif find_dup:current.next = current.next.nextreturn dummy.next

注意点
• A->B->C 删除 B,A.next = C
• 删除用一个 Dummy Node 节点辅助(允许头节点可变)
• 访问 X.next 、X.value 一定要保证 X != nil

reverse-linked-list

反转一个单链表。

  • 思路:将当前结点放置到头结点
class Solution:def reverseList(self, head: ListNode) -> ListNode:if head is None:return headtail = headwhile tail.next is not None:# put tail.next to head  tmp = tail.nexttail.next = tail.next.nexttmp.next = headhead = tmpreturn head
  • Recursive method is tricky
class Solution:def reverseList(self, head: ListNode) -> ListNode:if head is None or head.next is None:return headrev_next = self.reverseList(head.next)head.next.next = headhead.next = Nonereturn rev_next

reverse-linked-list-ii

反转从位置 mn 的链表。请使用一趟扫描完成反转。

  • 思路:先找到 m 处, 再反转 n - m 次即可
class Solution:def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:if head is None:return headn -= m # number of times of reversecurr = dummy = ListNode(next=head)while m > 1: # find node at m - 1curr = curr.nextm -= 1start = curr.nextwhile n > 0: # reverse n - m timestmp = start.nextstart.next = tmp.nexttmp.next = curr.nextcurr.next = tmpn -= 1return dummy.next

merge-two-sorted-lists

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

  • 思路:通过 dummy node 链表,连接各个元素
class Solution:def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:tail = dummy = ListNode()while l1 is not None and l2 is not None:if l1.val > l2.val:tail.next = l2l2 = l2.nextelse:tail.next = l1l1 = l1.nexttail = tail.nextif l1 is None:tail.next = l2else:tail.next = l1return dummy.next

partition-list

给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。

  • 思路:将大于 x 的节点,放到另外一个链表,最后连接这两个链表
class Solution:def partition(self, head: ListNode, x: int) -> ListNode:p = l = ListNode()q = s = ListNode(next=head)while q.next is not None:if q.next.val < x:q = q.nextelse:p.next = q.nextq.next = q.next.nextp = p.nextp.next = Noneq.next = l.nextreturn s.next

哑巴节点使用场景

当头节点不确定的时候,使用哑巴节点

sort-list

O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。

  • 思路:归并排序,slow-fast找中点
class Solution:def _merge(self, l1, l2):tail = l_merge = ListNode()while l1 is not None and l2 is not None:if l1.val > l2.val:tail.next = l2l2 = l2.nextelse:tail.next = l1l1 = l1.nexttail = tail.nextif l1 is not None:tail.next = l1else:tail.next = l2return l_merge.nextdef _findmid(self, head):slow, fast = head, head.nextwhile fast is not None and fast.next is not None:fast = fast.next.nextslow = slow.nextreturn slow# 找到中点,断开再合并def sortList(self, head: ListNode) -> ListNode:if head is None or head.next is None:return headmid = self._findmid(head)tail = mid.nextmid.next = None # break from middlereturn self._merge(self.sortList(head), self.sortList(tail))

注意点

  • 快慢指针 判断 fast 及 fast.Next 是否为 nil 值
  • 递归 mergeSort 需要断开中间节点
  • 递归返回条件为 head 为 nil 或者 head.Next 为 nil

reorder-list

给定一个单链表 LLL→…→L__nL
将其重新排列后变为: LL__nLL__nLL__n→…

  • 思路:找到中点断开,翻转后面部分,然后合并前后两个链表
class Solution:def reverseList(self, head: ListNode) -> ListNode:prev, curr = None, headwhile curr is not None:curr.next, prev, curr = prev, curr, curr.nextreturn prevdef reorderList(self, head: ListNode) -> None:"""Do not return anything, modify head in-place instead."""if head is None or head.next is None or head.next.next is None:returnslow, fast = head, head.nextwhile fast is not None and fast.next is not None:fast = fast.next.nextslow = slow.nexth, m = head, slow.nextslow.next = Nonem = self.reverseList(m)while h is not None and m is not None:p = m.nextm.next = h.nexth.next = mh = h.next.nextm = preturn

linked-list-cycle

给定一个链表,判断链表中是否有环。

  • 思路1:Hash Table 记录所有结点判断重复,空间复杂度 O(n) 非最优,时间复杂度 O(n) 但必然需要 n 次循环
  • 思路2:快慢指针,快慢指针相同则有环,证明:如果有环每走一步快慢指针距离会减 1,空间复杂度 O(1) 最优,时间复杂度 O(n) 但循环次数小于等于 n
    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
class Solution:def hasCycle(self, head: ListNode) -> bool:slow = fast = headwhile fast is not None and fast.next is not None:slow = slow.nextfast = fast.next.nextif fast == slow:return Truereturn False

linked-list-cycle-ii

给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null

  • 思路:快慢指针,快慢相遇之后,慢指针回到头,快慢指针步调一致一起移动,相遇点即为入环点。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

class Solution:def detectCycle(self, head: ListNode) -> ListNode:slow = fast = headwhile fast is not None and fast.next is not None:slow = slow.nextfast = fast.next.nextif slow == fast:slow = headwhile fast != slow:fast = fast.nextslow = slow.nextreturn slowreturn None

坑点

  • 指针比较时直接比较对象,不要用值比较,链表中有可能存在重复值情况
  • 第一次相交后,快指针需要从下一个节点开始和头指针一起匀速移动

注意,此题中使用 slow = fast = head 是为了保证最后找环起始点时移动步数相同,但是作为找中点使用时一般用 fast=head.Next 较多,因为这样可以知道中点的上一个节点,可以用来删除等操作。

  • fast 如果初始化为 head.Next 则中点在 slow.Next
  • fast 初始化为 head,则中点在 slow

palindrome-linked-list

请判断一个链表是否为回文链表。

  • 思路:O(1) 空间复杂度的解法需要破坏原链表(找中点 -> 反转后半个list -> 判断回文),在实际应用中往往还需要复原(后半个list再反转一次后拼接),操作比较复杂,这里给出更工程化的做法
class Solution:def isPalindrome(self, head: ListNode) -> bool:s = []slow = fast = headwhile fast is not None and fast.next is not None:s.append(slow.val)slow = slow.nextfast = fast.next.nextif fast is not None:slow = slow.nextwhile len(s) > 0:if slow.val != s.pop():return Falseslow = slow.nextreturn True

copy-list-with-random-pointer

给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。
要求返回这个链表的 深拷贝。

  • 思路1:hash table 存储 random 指针的连接关系
class Solution:def copyRandomList(self, head: 'Node') -> 'Node':if head is None:return Noneparent = collections.defaultdict(list)out = Node(0)o, n = head, outwhile o is not None:n.next = Node(o.val)n = n.nextif o.random is not None:parent[o.random].append(n)o = o.nexto, n = head, out.nextwhile o is not None:if o in parent:for p in parent[o]:p.random = no = o.nextn = n.nextreturn out.next
  • 思路2:复制结点跟在原结点后面,间接维护连接关系,优化空间复杂度,建立好新 list 的 random 链接后分离
class Solution:def copyRandomList(self, head: 'Node') -> 'Node':if head is None:return Nonep = headwhile p is not None:p.next = Node(p.val, p.next)p = p.next.nextp = headwhile p is not None:if p.random is not None:p.next.random = p.random.nextp = p.next.nextnew = head.nexto, n = head, newwhile n.next is not None:o.next = n.nextn.next = n.next.nexto = o.nextn = n.nexto.next = Nonereturn new

总结

链表必须要掌握的一些点,通过下面练习题,基本大部分的链表类的题目都是手到擒来~

  • null/nil 异常处理
  • dummy node 哑巴节点
  • 快慢指针
  • 插入一个节点到排序链表
  • 从一个链表中移除一个节点
  • 翻转链表
  • 合并两个链表
  • 找到链表的中间节点

练习

  • remove-duplicates-from-sorted-list
  • remove-duplicates-from-sorted-list-ii
  • reverse-linked-list
  • reverse-linked-list-ii
  • merge-two-sorted-lists
  • partition-list
  • sort-list
  • reorder-list
  • linked-list-cycle
  • linked-list-cycle-ii
  • palindrome-linked-list
  • copy-list-with-random-pointer

文章转载自:

http://xGUxW6Dp.xsymm.cn
http://0YMRbWPM.xsymm.cn
http://Agtx6EkO.xsymm.cn
http://EvluZb6a.xsymm.cn
http://bV6r4h2w.xsymm.cn
http://D874lJ1j.xsymm.cn
http://DPkZqAkh.xsymm.cn
http://pKheDFsS.xsymm.cn
http://KyUhnCTC.xsymm.cn
http://qTJsdSJ7.xsymm.cn
http://5KQkOFAm.xsymm.cn
http://ME4R4KU2.xsymm.cn
http://dM1BJjxl.xsymm.cn
http://K1HzEp3v.xsymm.cn
http://VqK1EF4m.xsymm.cn
http://cXKP1SVx.xsymm.cn
http://AAmeVChF.xsymm.cn
http://qwZrdQ9D.xsymm.cn
http://wUYRsMxa.xsymm.cn
http://mqE1fzLu.xsymm.cn
http://hN8WHMve.xsymm.cn
http://QvVMEGz7.xsymm.cn
http://102pIPC3.xsymm.cn
http://LDjokXhW.xsymm.cn
http://OB9Bi9I4.xsymm.cn
http://j70IbOlW.xsymm.cn
http://G6541yUK.xsymm.cn
http://9KmgsztU.xsymm.cn
http://HjfQv9Ux.xsymm.cn
http://A7cHLNb8.xsymm.cn
http://www.dtcms.com/wzjs/623604.html

相关文章:

  • 网站换行代码网页版qq音乐在线登录
  • 网站建设包含seo吗如何搭建公司网络
  • 湖北企业网站建设公司今天军事新闻最新消息视频
  • 无锡做网站公司多少钱呼和浩特网站建设
  • 法律顾问 网站 源码黑龙江省关于城市建设政策网站
  • 那种漂亮的网站怎么做设计师需要学历吗
  • 网站流量查询工具网页游戏奥奇传说
  • 长沙网页制作模板的网站wordpress 分类 id
  • 提供网站建设找哪家公司好wordpress多个博客
  • 标签化网站二级域名购买平台
  • 网站开发体会如何做网站建设团队建设
  • 电子商务网站建设信息智慧团建网站登录入口电脑版
  • 蓝色网站后台官网设计费用报价
  • 整站优化该怎么做炫酷的wordpress插件
  • 同ip怎么做不同的网站管理咨询公司起名大气上口的
  • 网页浏览器证书失效怎么修复长沙seo排名外包
  • 做网站多少钱特惠西宁君博s建网站是自己做还是用CMS
  • 阿里云域名注册好了怎么做网站优购商城网站建设
  • 微信网站制作平台给公司做个网站多少钱
  • 商务网站开发目的常德网站建设设计
  • 中国质量建设协会网站网站建设的多少钱
  • 网站建设河北石家庄全国广告公司网站建设
  • 做sorry动图的网站seo全网营销公司
  • 住房住房和城乡建设部网站wordpress站内统计插件
  • 嘉兴企业网站建设系统安全网多少钱一个
  • 企业网站建设 深圳广州注册公司在哪个网站
  • 怎么样上传网站资料偷wordpress模板
  • 吴桥网站建设公司flashfxp怎么做网站
  • 用python做网站怎么赚钱丹阳网站建设效果
  • 太原广告传媒有限公司seo研究