Python之链表

简介: 笔记

简单的实现了链表的增删改查功能

单链表


11.png

class Node(object):
    def __init__(self, data = -1, next = None):
        self.data = data
        self.next = next
class List(object):
    def __init__(self, head=Node(-1)):
        self.head = head
        self.length = 0
    def __del__(self):
        del self.head
        self.length = 0
    @classmethod
    # 看了 装饰器才懂, 这会记忆化!!!
    def create(cls):
        obj = cls()
        return obj
    @staticmethod
    def printf(head) -> bool:
        if head == None:
            return False
        tmp = head
        while tmp.next != None:
            tmp = tmp.next
            print(tmp.data, end='')
        return True
    def insert(self, index, val) -> bool:  # 位置 & 值
        if self.head == None or index < 0 or index > self.size():
            return False
        tmp = self.head
        while index > 0:
            tmp = tmp.next
            index -= 1
        node = Node(val, tmp.next)
        tmp.next = node
        self.length += 1
        return True
    def delete(self, index) -> bool:  # 位置
        if self.head == None or index < 0 or index >= self.size():
            return False
        tmp = self.head
        while index > 0:
            tmp = tmp.next
            index -= 1
        tmp.next = tmp.next.next
        self.length -= 1
        return True
    def modify(self, index, val) -> bool:  # 位置 & 值
        if self.head == None or index < 0 or index >= self.size():
            return False
        tmp = self.head
        while index >= 0:
            tmp = tmp.next
            index -= 1
        tmp.data = val
        return True
    def find(self, val) -> int:  # return 位置
        tmp = self.head
        now = 0
        while tmp.next != None:
            tmp = tmp.next
            if tmp.data == val:
                return now
            now += 1
        return -1
    def size(self) -> int:
        return self.length
    def empty(self) -> bool:
        return self.head.next == None
if __name__ == '__main__':
    lis = List.create()
    lis.insert(0, 1)
    lis.insert(0, 2)
    lis.insert(1, 3)
    lis.insert(0, 4)
    lis.delete(3)
    List.printf(lis.head)
    lis.find(2)

双链表


12.png

class Node(object):
    def __init__(self, val):
        self.val = val
        self.pre = None
        self.next = None
class List(object):
    def __init__(self, head = Node(-1), tail = Node(-1)):
        self.head = head
        self.tail = tail
        head.next = tail
        tail.pre = head
        self.length = 0
    def __del__(self):
        del self.head
        del self.tail
        self.length = 0
    def insert(self, index, val):
        if index < 0 or index > self.size() or self.head == None:
            return False
        tmp = Node(val)
        cur = self.head
        while index > 0:
            cur = cur.next
            index -= 1
        tmp.next = cur.next
        cur.next = tmp
        tmp.next.pre = tmp
        tmp.pre = cur
        self.length += 1
        return True
    def delete(self, index):
        if index < 0 or index >= self.size() or self.head == None:
            return False
        cur = self.head
        while index > 0:
            cur = cur.next
            index -= 1
        cur.next = cur.next.next
        cur.next.pre = cur
        self.length -= 1
        return True
    def modify(self, index, val):
        if index < 0 or index >= self.size() or self.head == None:
            return False
        cur = self.head
        while index >= 0:
            cur = cur.next
            index -= 1
        cur.val = val
        return True
    def find(self, val):
        if self.head == None:
            return -1
        cur = self.head
        cnt = 0
        while cur.next != self.tail:
            cur = cur.next
            if cur.val == val:
                return cnt
            cnt += 1
        return -1
    def printf(self):
        cur = self.head
        while cur.next != self.tail:
            cur = cur.next
            print(cur.val, end='')
    def size(self):
        return self.length
    def empty(self):
        return size() == 0
    if __name__ == '__main__':
    lis = List()
    lis.insert(0, 1)
    lis.insert(0, 2)
    lis.insert(2, 3)
    lis.insert(1, 4)
    # 2413
    lis.printf()
    lis.delete(2)
    lis.delete(2)
    print()
    lis.printf()
    lis.modify(1, 0)
    print()
    lis.printf()
    lis.find(4)

主要熟悉下python的数据结构写法,bug应该存在,不想调(~hhh~

相关文章
|
2月前
|
索引 Python
【Leetcode刷题Python】328. 奇偶链表
在不使用额外空间的情况下,将链表中的奇数和偶数索引节点重新排序的方法,并提供了相应的Python实现代码。
30 0
|
2月前
|
Python
【Leetcode刷题Python】25.K 个一组翻转链表
解决LeetCode "K 个一组翻转链表" 问题的三种方法:使用栈、尾插法和虚拟节点顺序法,并提供了每种方法的Python实现代码。
27 0
|
2月前
|
Python
【Leetcode刷题Python】114. 二叉树展开为链表
LeetCode上114号问题"二叉树展开为链表"的Python实现,通过先序遍历二叉树并调整节点的左右指针,将二叉树转换为先序遍历顺序的单链表。
23 3
【Leetcode刷题Python】114. 二叉树展开为链表
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
42 5
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 18. 删除链表的节点
Leetcode题目"剑指 Offer 18. 删除链表的节点"的Python解决方案,通过使用双指针法找到并删除链表中值为特定数值的节点,然后返回更新后的链表头节点。
33 4
|
2月前
|
存储 Python
【Leetcode刷题Python】23. 合并K个升序链表
合并K个升序链表的方法:使用数组排序的暴力求解法、使用小顶堆的高效方法,以及分而治之的策略,并提供了相应的Python实现代码。
16 1
|
2月前
|
Python
【Leetcode刷题Python】138. 复制带随机指针的链表
LeetCode上题目“138. 复制带随机指针的链表”的Python解决方案,包括两种方法:一种是在每个节点后复制一个新节点然后再分离出来形成新链表;另一种是构建一个字典来跟踪原始节点与其副本之间的映射关系,从而处理新链表的构建。
15 1
|
2月前
|
Python
【Leetcode刷题Python】234.回文链表
两种判断链表是否为回文的方法:使用栈和拆分为两个链表后反转对比,并给出了相应的Python代码实现。
17 0
|
2月前
|
Python
【Leetcode刷题Python】203.移除链表元素
文章提供了三种删除链表中特定值节点的方法:迭代法、虚拟节点迭代删除法和递归法,并给出了相应的Python实现代码。
17 0
|
2月前
|
Python
【Leetcode刷题Python】92.反转链表II
LeetCode上题目“92. 反转链表 II”的Python解决方案,其中包括两种方法:一种是头插法,另一种是迭代法。迭代法涉及先截取链表的一部分,然后反转这部分链表,最后将反转后的部分重新连接到原链表中。
15 0
下一篇
无影云桌面