试写出一个采用单链表存储的线性表A(A带表头结点 Head)的数据元素逆置的代码
时间: 2023-05-30 15:07:34 浏览: 82
单链表的逆置 代码
```python
def reverse_list(head):
if not head or not head.next:
return head
cur = head.next
prev = None
while cur:
temp = cur.next
cur.next = prev
prev = cur
cur = temp
head.next = prev
return head
```
注:上述代码中,head是带表头结点的单链表的头指针。
阅读全文