• 编程基础 —— 链表


    目录

    1 单链表的逻辑结构与存储结构

    2 单链表的定义

    3 插入元素

    4 删除元素

    5 创建单链表

    (1)尾插法

    (2)头插法

    6 双链表

    7 循环链表

    (1)循环单链表

    (2)循环双链表

    8 练习

    (1)移除链表元素

    (2)旋转链表

    1 单链表的逻辑结构与存储结构

    • 逻辑结构:数据元素之间的逻辑关系
      • 集合、线性结构(一对一)、树形结构(一对多)、图结构(多对多)
    • 存储结构:顺序存储、链式存储、索引存储、散列存储
      • 顺序存储(顺序表):逻辑上相邻的元素物理位置也相邻
      • 链式存储(单链表):逻辑上相邻的元素物理位置不一定相邻

    2 单链表的定义

    1. class ListNode:
    2. def __init__(self, val = 0, next = None):
    3. self.val = val
    4. self.next = next
    • 带头节点的单链表(写代码方便)
    • 不带头节点的单链表(写代码麻烦)

    3 插入元素

    1. # 在第i个位置插入elem
    2. def Inster(head, i, elem):
    3. assert i >= 0
    4. cur = head
    5. while i != 0:
    6. i -= 1
    7. cur = cur.next
    8. if not cur:
    9. return False
    10. temp = cur.next
    11. cur.next = elem
    12. elem.next = temp
    13. return True

    4 删除元素

    1. def ListDelete(head, i):
    2. assert i >= 0
    3. cur = head
    4. while i >= 0:
    5. i -= 1
    6. cur = cur.next
    7. if not cur.next:
    8. return False
    9. cur.next = cur.next.next
    10. return True

    5 创建单链表

    (1)尾插法

    1. def BulidLink_Head(l):
    2. head = ListNode()
    3. temp = head
    4. for elem in l:
    5. temp.next = ListNode(elem)
    6. temp = temp.next
    7. return head
    8. head = BulidLink_Tail([1,2,3,4])
    9. while head.next:
    10. head = head.next
    11. print(head.val)

    (2)头插法

    1. def BulidLink_Head(l):
    2. head = ListNode()
    3. temp = head
    4. for elem in l:
    5. temp = head.next
    6. head.next = ListNode(elem, temp)
    7. return head

    6 双链表

    解决单链表无法逆向索引的问题

    1. class DLinkNode:
    2. def __init__(self, val = 0, next = Node, prior):
    3. self.val = val
    4. self.next = next
    5. self.prior = prior

    7 循环链表

    (1)循环单链表

    从一个结点出发可以找到其他任何结点

    (2)循环双链表

    从头找到尾和从尾找到头的时间复杂度都是O(1)

    8 练习

    (1)移除链表元素

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

    1. class solution:
    2. def removeElements(self, head, val):
    3. head = ListNode(next = head)
    4. pre = head
    5. while pre.next:
    6. if pre.next.val == val:
    7. pre.next = pre.next.next
    8. else:
    9. pre = pre.next
    10. return head.next

    (2)旋转链表

    一个链表的头节点head ,旋转链表;将链表每个节点向右移动k个位置。

    1. class solution:
    2. def rotateRight(self, head, k):
    3. if not head:
    4. return None
    5. length = 0
    6. temp = head
    7. while temp.next:
    8. length += 1
    9. temp = temp.next
    10. temp.next = head
    11. k = k % (length + 1)
    12. temp = head
    13. for i in range(length - k):
    14. temp = temp.next
    15. head = temp.next
    16. temp.next = None
    17. return head
  • 相关阅读:
    火山引擎DataLeap的数据血缘用例与设计概述
    UDP 丢包的艺术
    【Visual Leak Detector】源码调试 VLD 库
    多商户商城系统功能拆解27讲-平台端分销结算设置
    acwing算法提高之图论--无向图的双连通分量
    Stable Diffusion AI绘图
    HC32F120开发之IAP
    Android学习笔记 51. 项目发布
    机器学习(九)归纳总结DLC
    rmq文件清理策略
  • 原文地址:https://blog.csdn.net/waywardG/article/details/125628591