python实现单链表及常用方法->判空|长度|头尾及指定插入|删除|搜索|反转

# -*- coding: utf-8 -*-

class Node:
    """ 节点类 """
    def __init__(self, data):
        self.data = data
        self.next = None

class SingleListNode:
    """ 单链表 """
    def __init__(self):
        """" 初始化单链表 """
        self.head = None

    def is_empty(self):
        """" 链表是否为空 """
        return self.head is None

    def lenth(self):
        """ 链表长度 """
        cur = self.head
        cnt = 0
        while cur is not None:
            cnt += 1
            cur = cur.next
        return cnt

    def add_first(self, data):
        """ 头部插入 """
        node = Node(data)
        node.next = self.head
        self.head = node

    def add_last(self, data):
        """ 尾部插入 """
        node = Node(data)
        if self.is_empty():
            self.head = node
        else:
            cur = self.head
            while cur.next is not None:
                cur = cur.next
            cur.next = node

    def insert_node(self, index, data):
        """ 指定位置插入 """
        node = Node(data)
        if index < 0 or index > self.lenth():
            return False
        elif index == 0:
            self.add_first(data)
        elif index == self.lenth():
            self.add_last(data)
        else:
            cur = self.head
            count = 0
            while count < index - 1:
                cur = cur.next
                count += 1
            node.next = cur.next
            cur.next = node
        return True

    def remove_node(self, data):
        """ 删除节点 """
        cur = self.head
        pre = None
        if self.head == data:
            self.head.next = self.head
        else:
            while cur.data != data:
                pre = cur
                cur = cur.next
            pre.next = cur.next

    def is_exist(self, data):
        """ 判断节点是否存在 """
        cur = self.head
        while cur is not None:
            if cur.data == data:
                return True
            else:
                cur = cur.next
        return False

    def traversal(self):
        """ 遍历单链表 """
        cur = self.head
        while cur is not None:
            print(cur.data, end=' ')
            cur = cur.next

    def reverse(self):
        if self.head is None or self.head.next is None: #空链表 or 单节点,返回head
            return self.head
        else:
            prev = None
            while self.head is not None:
                temp = self.head.next #记录下个节点

                self.head.next = prev #当前节点指向空
                prev = self.head      #prev即为当前节点

                self.head = temp      #移动当前指针,为下轮做准备

            # 打印反转链表
            cur = prev
            while cur is not None:
                print(cur.data, end=' ')
                cur = cur.next

            return prev




if __name__ == "__main__":
    listnode = SingleListNode()
    listnode.add_first(2)
    listnode.add_first(1)
    listnode.add_last(5)
    listnode.insert_node(2, 3)
    listnode.traversal() #遍历节点, 1 2 3 5
    print()
    print(listnode.is_empty()) #是否为空
    print(listnode.lenth()) #链表长度
    listnode.remove_node(3) #删除值为3节点
    print(listnode.is_exist(3)) #值为3的节点是否存在
    listnode.traversal() #遍历节点 1 2 5
    print()
    listnode.reverse() #反转链表 5 2 1

原文地址:https://www.cnblogs.com/davis12/p/15346947.html