用python写llm简单的代码
时间: 2024-10-11 19:06:11 浏览: 30
在Python中,我们可以使用内置的`list`数据结构来创建简单链表(Linked List Management)。链表是一种线性的数据结构,其中每个节点包含一个元素值和指向下一个节点的引用。下面是一个简单的单向链表的例子:
```python
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
# 添加节点到链表的开头
def append(self, data):
if not self.head:
self.head = Node(data)
else:
new_node = Node(data)
new_node.next = self.head
self.head = new_node
# 添加节点到链表的末尾
def add_at_end(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
# 删除指定值的节点(如果存在)
def remove(self, value):
if not self.head:
return
if self.head.data == value:
self.head = self.head.next
return
current = self.head
while current.next and current.next.data != value:
current = current.next
if current.next:
current.next = current.next.next
# 使用示例
linked_list = LinkedList()
linked_list.append(1)
linked_list.append(2)
linked_list.add_at_end(3)
linked_list.remove(2)
阅读全文