链表-查找链表最后节点函数pta
时间: 2024-01-04 21:19:31 浏览: 150
链表操作节点
根据提供的引用内容,以下是一个查找链表最后节点的函数的示例代码:
```python
class Node:
def __init__(self, value):
self.value = value
self.next = None
def find_last_node(head):
if head is None:
return None
current = head
while current.next is not None:
current = current.next
return current
# 示例链表
head = Node(1)
node2 = Node(2)
node3 = Node(3)
head.next = node2
node2.next = node3
last_node = find_last_node(head)
print("The value of the last node is:", last_node.value) # 输出:The value of the last node is: 3
```
该函数通过遍历链表,直到找到最后一个节点,并返回该节点的值。如果链表为空,则返回None。
阅读全文