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

c网站制作旅游网站建设1000字

c网站制作,旅游网站建设1000字,网站综合排名信息查询,天津广告设计公司排名1.相交链表 问题:给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。 方法:分别遍历两个链表(前后交替) # 分别遍历两个链表找…

1.相交链表

问题:给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。

方法:分别遍历两个链表(前后交替)

# 分别遍历两个链表找交点
# 如果有交点,必然在某个位置相遇,因为交点及以后是重合的部分
# 如果无交点,也必然同时结束(都是遍历两个链表)
def getIntersectionNode(headA, headB):pA, pB = headA, headB# 遍历找交点while pA != pB:pA = pA.next if pA else headB   # 遍历完A,遍历BpB = pB.next if pB else headA   # 遍历完B,遍历Areturn pA                           # 交点/None

2.反转链表

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

方法:画图

一定要画图!!!

# 画图!!!
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = next# 创建链表(虚拟节点)
def CreateList(nums):dummy = ListNode()p = dummyfor num in nums:p.next = ListNode(num)p = p.nextreturn dummy.next           # 返回头结点# # 创建链表(自实现)
# def CreateList(nums):
#     head = ListNode(nums[0])
#     p = head
#     for i in range(1, len(nums)):
#         p.next = ListNode(nums[i])
#         p = p.next
#     return head# 输出链表
def Print(p):while p:print(p.val)p = p.next# 反转链表(画图!!!)
def Reverse(head):pre, cur = None, headwhile cur:temp = cur.nextcur.next = prepre, cur = cur, tempreturn prehead = CreateList([1,2,3,4,5])
Print(head)
re_head = Reverse(head)
Print(re_head)

3.回文链表

方法1:原链表 == 反转链表

# 方法1:原链表 == 反转链表(注意要对原链表进行备份,链表反转之后,原链表也会跟着改变)
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = next# 创建链表
def CreateList(nums):dummy = ListNode()p = dummyfor num in nums:p.next = ListNode(num)p = p.nextreturn dummy.nextdef Print(p):while p:print(p.val)p = p.next# 链表备份
def CopyList(pA):dummy = ListNode()pB = dummywhile pA:pB.next = ListNode(pA.val)pB = pB.nextpA = pA.nextreturn dummy.next# 翻转
def Reverse(head):pre, cur = None, headwhile cur:temp = cur.nextcur.next = prepre, cur = cur, tempreturn pre# 判断回文
def IsPalind(pA, pB):while pA and pB:if pA.val != pB.val:return FalsepA, pB = pA.next, pB.nextif pA or pB:return Falsereturn Truehead = CreateList([1,2,2,1])
Print(head)
copy_head = CopyList(head)
re_head = Reverse(head)
Print(re_head)
print(IsPalind(copy_head, re_head))

方法2:转数组 + 双指针

# 方法2:转数组 + 双指针
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = next# 创建链表
def CreateList(nums):dummy = ListNode()p = dummyfor num in nums:p.next = ListNode(num)p = p.nextreturn dummy.nextdef Print(p):while p:print(p.val)p = p.next# 判断回文(链表转数组,双指针直接判断前后元素)
def IsPalind(p):# 转数组nums = []while p:nums.append(p.val)p = p.nextleft, right = 0, len(nums)-1# 判断数组回文while left < right:if nums[left] != nums[right]:return Falseleft += 1right -= 1return Truehead = CreateList([1,2,2,1])
Print(head)
print(IsPalind(head))

方法3:快慢指针 + 反转

# 方法3:快慢指针 + 反转
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = next# 创建链表
def CreateList(nums):dummy = ListNode()p = dummyfor num in nums:p.next = ListNode(num)p = p.nextreturn dummy.nextdef Print(p):while p:print(p.val)p = p.next# 翻转
def Reverse(head):pre, cur = None, headwhile cur:temp = cur.nextcur.next = prepre, cur = cur, tempreturn pre# 判断回文(快慢指针 + 反转)
def IsPalind(head):slow, fast = head, head        # 快慢指针(定位)while fast and fast.next:slow, fast = slow.next, fast.next.next# 反转(后半部分)re_slow = Reverse(slow)# 比较前后部分while re_slow:if head.val != re_slow.val:return Falsehead, re_slow = head.next, re_slow.nextreturn Truehead = CreateList([1,2,2,1])
# Print(head)
print(IsPalind(head))


文章转载自:

http://zHiwAVIs.hcstr.cn
http://7FYTjX0j.hcstr.cn
http://wfQKFuqq.hcstr.cn
http://dLAo7Vsb.hcstr.cn
http://Az5g2AXN.hcstr.cn
http://WTbuHj6a.hcstr.cn
http://I1zMdAdc.hcstr.cn
http://chtWwCWb.hcstr.cn
http://rzgMKU6W.hcstr.cn
http://almO0o0F.hcstr.cn
http://VZiEhLGI.hcstr.cn
http://7BmgoM9C.hcstr.cn
http://nY84HdLP.hcstr.cn
http://RFCPwy2J.hcstr.cn
http://RGKrDMFd.hcstr.cn
http://bOf650ny.hcstr.cn
http://Y9LB2RZF.hcstr.cn
http://kKpPyJsb.hcstr.cn
http://m1wjV4CK.hcstr.cn
http://OlAMZgVP.hcstr.cn
http://bow55Biq.hcstr.cn
http://SWAFd3WG.hcstr.cn
http://Cb5vbH2M.hcstr.cn
http://sSOtJSBh.hcstr.cn
http://ZtVuaPyL.hcstr.cn
http://eW2DRVfu.hcstr.cn
http://9cfWlSed.hcstr.cn
http://q14WEQUE.hcstr.cn
http://U5JV6m6L.hcstr.cn
http://nmE0jGCt.hcstr.cn
http://www.dtcms.com/wzjs/720534.html

相关文章:

  • 网站备案有什么要求吗东莞外贸网站建设
  • 石龙网站设计广东专业做网站排名哪家好
  • 江苏九天建设有限公司网站wordpress 内存使用教程
  • 用wordpress建医疗网站python在线编程平台
  • 酷虎云建站工具垫江网站建设费用
  • 娱乐网站设计多少行业wordpress 上传目录权限设置
  • 网站软文推广好处网站建设怎么下载代码
  • 群晖wordpress站点地址注册一个免费的网站
  • jsp网站模版做外贸网站需要什么卡
  • 个人网站可以如果做淘宝客wordpress修改工具
  • 建站视频百度推广需要自己做网站吗
  • 做的好的个人网站知乎哈尔滨信息网免费招聘
  • 网页设计与网站建设完全学习手册semiconductor是什么意思
  • 新泰网站制作建材类网站建设需要的资料
  • 权重较高网站公司管理系统叫什么
  • 北京网站托管的公司南平建设集团网站
  • 客户端网站建设文档哪有做网站的公司
  • 网站建设详细流网站后台上传内容前台首页不显示
  • 冠县 网站建设企业网站建设实训总结
  • 南京制作网站如何做网页跳转
  • 网站备案提示wordpress调用制定id
  • 徐州企业网站设计校园跑腿小程序源码
  • 重庆地产网站建设方案许昌网站建设汉狮怎么样
  • 长沙网站推广平台龙岩会员系统小程序定制开发
  • 学建设网站去哪里学有哪些网站交互效果做的好的
  • 个人公司网站搭建如何做配音网站
  • 古典家具公司网站模板wordpress 升级后 插件
  • 莆田网站建设推广网站备案 不备案
  • 做前后端网站教程深圳市城市建设管理局
  • 网站后台如何修改新闻发布时间oecms(php企业网站管理系统)