python创建链表的两种形式

2025-12-01 0 61,559

说明

1、头插法将结点插入头结点后面,新加入的结点next指向原来head指向的结点。

head改为新的结点。

2、尾插法将结点插入尾点前,新节点的next指向tail,tail更新为新节点。

实例

class Node:
    def __init__(self,item):
        self.item = item
        self.next = None
 
class HandleNode:
    def create_linklist_head(self,li):
        head = Node(li[0])
        for element in li[1:]:
            node = Node(element)
            node.next = head
            head = node
        return head
 
    def create_linklist_tail(self,li):
        head = Node(li[0])
        tail = head
        for element in li[1:]:
            node = Node(element)
            tail.next = node
            tail = node
        return head
 
    def print_linklist(self,head):
        while head:
            print(head.item,end=',')
            head=head.next

以上就是python创建链表的两种形式,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 python基础 python创建链表的两种形式 https://www.zhiup.top/792.html

相关