栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

链表的python实现

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

链表的python实现

单链表的增删改查

class ListNode(object):
    def __init__(self, value=0, next=None):
        self.value = value
        self.next = next


def BuildListNode(l: list):
    head = ListNode()
    temp = head
    for elem in l:
        temp.next = ListNode(elem)
        temp = temp.next
    return head


def BuildListNodeReverse(l: list):
    head = ListNode()
    for elem in l:
        temp = head.next
        head.next = ListNode(elem, temp)
    return head


def PrintListNode(head: ListNode):
    cur = head
    print('_________')
    while cur.next:
        cur = cur.next
        print(cur.value)


def Insert(head: ListNode, i, elem):
    assert i >= 0
    elem = ListNode(elem)
    cur = head
    while i != 0:
        i -= 1
        cur = cur.next
        if not cur:
            return False
    temp = cur.next
    cur.next = elem
    elem.next = temp
    return True


def Find(head: ListNode, i):
    assert i >= 0
    cur = head.next
    while i != 0:
        cur = cur.next
        i -= 1
        if not cur:
            return False
    return cur.value


def Delete(head: ListNode, i):
    assert i >= 0
    cur = head
    while i != 0:
        cur = cur.next
        i -= 1
        if not cur:
            return False
    cur.next = cur.next.next
    return True
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/916078.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号