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

python学习2-数据结构与算法-链表

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。


链表分类:
1.单向或双向
2.带头和不带头
3.循环或非循环

单链表的定义
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class NodeList(object):
    def __init__(self):
        self.head = Node()
    def getLen(self):
        if !self.head:
            return 0
        num = 1
        while !cur.next:
            num += 1
            cur = cur.next
        return num
    #打印链表
    def printList(self):
        if !self.head:
            print("none")
        ans = []
        cur = self.head
        while !cur:
            ans.append(curl.val)
            cur = cur.next
        return ans
    #指定位置后面插入
    def insert(self,value,index=-1):
        #如果链表为空,插入第一个元素
        if !self.head:
            self.head = Node(value)
            if index!=-1 or index!=0:
                return False
            return True
        #若指定插入位置为第一位
        if index == 0:
            cur = Node(value)
            cur.next = self.head
            self.head = cur
            return True
        #若指定位置为链表尾
        elif index == -1:
            cur = self.head
            while cur.next:
                cur = cur.next
            cur.next = Node(value)
        else:
            #index从0开始,到index-1的位置,需要移动index-1次
            i = index -1
            cur = self.head
            while i>0 and !cur.next:
                i -= 1
                cur = cur.next
            if !cur.next and i == 0:
                tmp = cur.next
                cur.next = Node(value)
                cur = cur.next
                cur.next = tmp
            #插入位置为链表尾
            elif i == 0 and !cur.next:
                cur.next = Node(value)
            else:
                return "insert index is too large"
            return self.head
    #指定位置后面删除
    def pop(self,value,index=-1):
        if !self.head:
            return False
        elif !self.head.next:
            if self.head.val != value:
                return False
            else:
                self.head = None
                return True
        else:
            cur ,pre = self.head,None
            while cur:
                if cur.val == value:
                    if cur == self.head:
                        self.head = cur.next
                    else:
                        pre.next = cur.next
                    break
                 else:
                    pre ,cur = cur,cur.next
            return True

Leetcode刷题

反转整个链表(面试高频考点)- - 力扣(LeetCode)206
回文链表(数组反转ans[::-1]). - 力扣(LeetCode)234
环形链表(快慢指针). - 力扣(LeetCode)141、142
链表排序148
合并K个升序链表. - 力扣(LeetCode)23
链表每K个节点翻转. - 力扣(LeetCode)24、. - 力扣(LeetCode)25
深拷贝. - 力扣(LeetCode)138
移除链表元素. - 力扣(LeetCode)19、

http://www.lryc.cn/news/392153.html

相关文章:

  • 项目一 nfs 共享服务器 Haproxy 代理 Keepalive 高可用集群
  • TCP粘包解决方法
  • 高职人工智能专业实训课之“生成对抗网络(GAN)”
  • 【MySQL系列】隐式转换
  • 亿发:信息化建设or面子工程?究竟什么才是真正的信息化解决方案
  • 【微信小程序开发实战项目】——如何制作一个属于自己的花店微信小程序(1)
  • 树形结构C语言的实现
  • 小程序渗透测试的两种方法——burpsuite、yakit
  • 代码随想录训练营Day56
  • S32K3 工具篇4:如何在S32DS中使用lauterbach下载
  • 深度神经网络语言识别
  • STM32自己从零开始实操07:电机电路原理图
  • 网页计算器的实现
  • JAVA设计模式-监听者模式
  • anaconda命令大全
  • “论单元测试方法及应用”写作框架,软考高级论文,系统架构设计师论文
  • 基于布雷格曼偏差校正技术的全变分一维时间序列信号降噪方法(MATLAB R2018A)
  • 【CentOS 7.6】Linux版本 portainer本地镜像导入docker安装配置教程,不需要魔法拉取!(找不着镜像的来看我)
  • 【windows|012】光猫、路由器、交换机详解
  • Node之Web服务
  • [Day 24] 區塊鏈與人工智能的聯動應用:理論、技術與實踐
  • 计算机图形学入门25:BRDF的测量
  • 空调计费系统是什么,你知道吗
  • 震惊!张宇25版高数18讲发布,656页惹争议!
  • React+TS前台项目实战(二十三)-- 基于属性自定义数值显示组件Decimal封装
  • pip install包出现哈希错误解决
  • 多线程压测方法模板
  • Uniapp软件库全新带勋章功能(包含前后端源码)
  • 秋招突击——7/5——设计模式知识点补充——适配器模式、代理模式和装饰器模式
  • bmob Harmony鸿蒙快速开发搜索功能