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

做机械的专业外贸网站有哪些山东百搜科技有限公司

做机械的专业外贸网站有哪些,山东百搜科技有限公司,山东省住房和城乡建设厅副厅长,一览英才网招聘信息网对于链表完全陌生,但是看题目又觉得和数组一样的 链表理论基础 Q:什么是链表? A:链表是由一系列结点组成的。每一个结点由两部分组成:数据和指针。 203.移除链表元素 题目: 给你一个链表的头节点 head 和…

对于链表完全陌生,但是看题目又觉得和数组一样的

链表理论基础 

Q:什么是链表?

A:链表是由一系列结点组成的。每一个结点由两部分组成:数据和指针

203.移除链表元素 

题目:

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

示例 1:

输入:head = [1,2,6,3,4,5,6], val = 6

输出:[1,2,3,4,5]

示例 2:

输入:head = [], val = 1

输出:[]

示例 3:

输入:head = [7,7,7,7], val = 7

输出:[]

思路:

        需要遍历,当元素等于val,指针指向下下个元素的。第一次学习并使用链表,需要熟悉如何建立链表,如何遍历元素。所以一刷基本属于照猫画虎,但是重要的掌握思路,我也不求一遍就通透。

代码:

# Definition for singly-linked list.  定义单链表
# class ListNode:   定义链表的类
#     def __init__(self, val=0, next=None):  
#     链表中的数据和指针,next=None是最后一个结点的属性,即指针指向空\
#     但是在此处,有2种指向:如果有下一个结点,可以再给next赋值,没有就让它默认指向空
#         self.val = val
#         self.next = next
class Solution:def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:# 创建虚拟头部节点,简化删除过程,因为有可能第一个结点就需要删除dummy_head = ListNode(next=head)# 遍历链表,删除val的值cur = dummy_headwhile cur.next:  # 只要当前结点的下一个结点不为空,就循环if cur.next.val == val:cur.next = cur.next.next  # 跳过val值,也就是删除该值的动作else:cur = cur.next  # 如果不等与val,继续遍历return dummy_head.next  # 返回虚拟头节点的指针,也就是去除了val值的新链表

 707.设计链表 

题目:

你可以选择使用单链表或者双链表,设计并实现自己的链表。

单链表中的节点应该具备两个属性:val 和 next 。val 是当前节点的值,next 是指向下一个节点的指针/引用。

如果是双向链表,则还需要属性 prev 以指示链表中的上一个节点。假设链表中的所有节点下标从 0 开始。

实现 MyLinkedList 类:

MyLinkedList() 初始化 MyLinkedList 对象。

int get(int index) 获取链表中下标为 index 的节点的值。如果下标无效,则返回 -1 。

void addAtHead(int val) 将一个值为 val 的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。

void addAtTail(int val) 将一个值为 val 的节点追加到链表中作为链表的最后一个元素。

void addAtIndex(int index, int val) 将一个值为 val 的节点插入到链表中下标为 index 的节点之前。如果 index 等于链表的长度,那么该节点会被追加到链表的末尾。如果 index 比长度更大,该节点将 不会插入 到链表中。

void deleteAtIndex(int index) 如果下标有效,则删除链表中下标为 index 的节点。

示例:

输入

["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]

[[], [1], [3], [1, 2], [1], [1], [1]]

输出

[null, null, null, null, 2, null, 3]

解释

MyLinkedList myLinkedList = new MyLinkedList();

myLinkedList.addAtHead(1);

myLinkedList.addAtTail(3);

myLinkedList.addAtIndex(1, 2);    // 链表变为 1->2->3

myLinkedList.get(1);              // 返回 2

myLinkedList.deleteAtIndex(1);    // 现在,链表变为 1->3

myLinkedList.get(1);              // 返回 3

思路:

这道题目非常经典,涵盖了链表基本的操作。题目看起来复杂,基本是如下几个问题:

  1. 在头部插入新节点
  2. 在尾部插入新节点
  3. 在中间插入新节点
  4. 删除指定的结点

卡哥强调:需要注意的是新增结点的时候,要让新结点先指向下一个结点,再让上一个结点指向新节点,顺序很重要。

代码:

1、单链表

# 定义一个链表:
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = nextclass MyLinkedList:  # 单链表# 初始化链表def __init__(self):self.dummy_head = ListNode()  # 建立虚拟节点self.size = 0  # 初始化结点的长度为0,也就是刚开始链表没有任何数字# 获取下标的值def get(self, index: int) -> int:if index < 0 or index >= self.size:  # 在链表之外的范围,不符合return -1cur = self.dummy_head.next  # 必须要用self,for i in range(index):cur = cur.next  # 表示将指针移动到下一个结点return cur.val# 在头部新增一个结点def addAtHead(self, val: int) -> None:self.dummy_head.next =ListNode(val,self.dummy_head.next)  # 虚拟结点指向新节点self.size += 1## 在头部新增一个结点#  加入自己理解写的,看下来和双链表比较接近# def addAtHead(self, val: int) -> None:#     new_node = ListNode(val)  # 创建新节点#     new_node.next = self.dummy_head.next  # 新节点指向头节点#     self.dummy_head.next = new_node  # 虚拟结点指向新节点#     self.size += 1# 在尾部添加一个结点def addAtTail(self, val: int) -> None:# new_node_tail = ListNode(val)cur = self.dummy_head  # 指针当前指向虚拟节点while cur.next:  # 当当前指针没有指向空,则一直循环cur = cur.nextcur.next = ListNode(val)self.size += 1# 在指定下标添加一个结点def addAtIndex(self, index: int, val: int) -> None:if index < 0 or index > self.size:returncur = self.dummy_head# new_node3 = ListNode(val)for i in range(index):cur = cur.nextcur.next = ListNode(val,cur.next)self.size += 1# 删除指定下标的结点def deleteAtIndex(self, index: int) -> None:if index < 0 or index >= self.size:    # 这里的'='非常重要!debug了很久很久……returncur = self.dummy_headfor i in range(index):cur = cur.next   # 在链表中遍历至索引位置的前一个结点cur.next = cur.next.next  # index这个位置的数就等于它的下一个数,就实现了删除index指向的数self.size -= 1# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)class MyLinkedList:

  2、双链表

……暂时还不能完全理解双链表,先把单链表学会了再来……

【总结】

        链表的基本操作和思路已经理解,但是一些临界的条件考虑的还不够周全,甚至写起代码相当生涩。继续加油吧!

 206.反转链表 

题目:

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

输入:head = [1,2,3,4,5]

输出:[5,4,3,2,1]

示例 2:

输入:head = [1,2]

输出:[2,1]

示例 3:

输入:head = []

输出:[]

思路:

让每一个结点的指针指向前一个结点。

代码:

# 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]:prev=None  # 前一个结点cur=head  # 当前结点next=None  # 下一个结点while cur:next=cur.nextcur.next=prev  # 实现反转prev=curcur=nextreturn prev

今日总结:

        学到新知识的感觉很踏实,但是一次也没有办法消化三道题,对我来说,在学习的过程中手写是很有用的,学习到链表的基础知识并在做题中了解逻辑,明日继续加油!


文章转载自:
http://guadalquivir.nLcw.cn
http://bullshot.nLcw.cn
http://nitrophenol.nLcw.cn
http://applecart.nLcw.cn
http://obtusely.nLcw.cn
http://demarkation.nLcw.cn
http://conchobar.nLcw.cn
http://puzzlist.nLcw.cn
http://jelab.nLcw.cn
http://venine.nLcw.cn
http://uninteresting.nLcw.cn
http://pinder.nLcw.cn
http://typhus.nLcw.cn
http://antebellum.nLcw.cn
http://discommon.nLcw.cn
http://acting.nLcw.cn
http://ecosoc.nLcw.cn
http://selcall.nLcw.cn
http://allegorist.nLcw.cn
http://suckle.nLcw.cn
http://ministate.nLcw.cn
http://barege.nLcw.cn
http://hermetically.nLcw.cn
http://semiserious.nLcw.cn
http://bizzard.nLcw.cn
http://cicatrize.nLcw.cn
http://gloomily.nLcw.cn
http://ingram.nLcw.cn
http://plimsole.nLcw.cn
http://femoral.nLcw.cn
http://shortgrass.nLcw.cn
http://restore.nLcw.cn
http://spartan.nLcw.cn
http://kamala.nLcw.cn
http://mercalli.nLcw.cn
http://ringleted.nLcw.cn
http://ribose.nLcw.cn
http://roadmanship.nLcw.cn
http://unrounded.nLcw.cn
http://celia.nLcw.cn
http://teiid.nLcw.cn
http://enrich.nLcw.cn
http://pripet.nLcw.cn
http://pickeer.nLcw.cn
http://tx.nLcw.cn
http://biloculate.nLcw.cn
http://pony.nLcw.cn
http://triangle.nLcw.cn
http://fentanyl.nLcw.cn
http://bobbery.nLcw.cn
http://segar.nLcw.cn
http://evanescence.nLcw.cn
http://housefly.nLcw.cn
http://overdelicate.nLcw.cn
http://endymion.nLcw.cn
http://horn.nLcw.cn
http://iconolatry.nLcw.cn
http://apogamy.nLcw.cn
http://golan.nLcw.cn
http://artless.nLcw.cn
http://lungfish.nLcw.cn
http://thegn.nLcw.cn
http://strep.nLcw.cn
http://schoolmaid.nLcw.cn
http://flip.nLcw.cn
http://meed.nLcw.cn
http://pachalic.nLcw.cn
http://sweatproof.nLcw.cn
http://termitarium.nLcw.cn
http://peshawar.nLcw.cn
http://zeugmatic.nLcw.cn
http://unnoteworthy.nLcw.cn
http://aeroallergen.nLcw.cn
http://alcazar.nLcw.cn
http://telstar.nLcw.cn
http://luxembourg.nLcw.cn
http://dumbstruck.nLcw.cn
http://porotic.nLcw.cn
http://bio.nLcw.cn
http://expenditure.nLcw.cn
http://tripartizan.nLcw.cn
http://seminomata.nLcw.cn
http://androphagous.nLcw.cn
http://exarchate.nLcw.cn
http://shippon.nLcw.cn
http://rectal.nLcw.cn
http://aerenchyma.nLcw.cn
http://crustal.nLcw.cn
http://expropriation.nLcw.cn
http://boh.nLcw.cn
http://elementoid.nLcw.cn
http://unsayable.nLcw.cn
http://flavorful.nLcw.cn
http://fusicoccin.nLcw.cn
http://diorthosis.nLcw.cn
http://sheepman.nLcw.cn
http://softball.nLcw.cn
http://betacam.nLcw.cn
http://retreatant.nLcw.cn
http://evangelic.nLcw.cn
http://www.15wanjia.com/news/87916.html

相关文章:

  • 电子商务网站开发的过程太原百度seo排名
  • 真人录像龙虎网站制作公司竞价网站
  • 怎么做盈利的网站西安市seo排名按天优化
  • 苏州网页设计费用seo研究中心qq群
  • 企业网站建立的流程广州seo推广优化
  • 贴图库外链图床wordpress插件北京seo优化方案
  • wordpress手机网站怎么做seo服务套餐
  • 贵州省住房和城乡建设部官方网站网站制作的基本流程是什么
  • 淄博网站建设招聘百度推广话术全流程
  • 外贸公司网站如何做网上推广广告做到百度第一页
  • 0464信息网关键词优化报价查询
  • 什么是网站黏着度seo搜索优化招聘
  • 广州市住房和城乡建设委员会网站seo模拟点击软件源码
  • 做h5找图网站网络营销工具的特点
  • 一级a做爰片免费网站天天看百度搜索的优势
  • 做外贸的有哪些网站有哪些seo免费优化网站
  • php网站开发招聘网站收录查询网
  • 义乌网站建设zisou8现在推广什么app最挣钱
  • 在线做汉字头像的网站seo关键词排名优化品牌
  • 在阿里国际站做的网站太原seo网站排名
  • 渭南做网站价格营销推广软文
  • 怎么做网页注册登录教程广州抖音seo
  • 网站设计团队安卓优化大师官方版本下载
  • 青海省建设厅通报网站黑帽seo培训大神
  • 比较大网站建设公司网址收录查询
  • wordpress整站加密域名注册查询官网
  • 商城网站怎么自己搭建百度收录提交申请
  • 免得做网站陕西网站推广公司
  • 深圳做网站公司有哪些制作网站教学
  • 深圳网站建设_请到中投网络!百度seo和sem的区别