今天爱分享给大家带来python如何翻转一个单链表【面试题详解】【附代码】,希望能够帮助到大家。
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
# 申请两个节点,pre和 cur,pre指向None
pre = None
cur = head
# 遍历链表,while循环里面的内容其实可以写成一行
# 这里只做演示,就不搞那么骚气的写法了
while cur:
# 记录当前节点的下一个节点
tmp = cur.next
# 然后将当前节点指向pre
cur.next = pre
# pre和cur节点都前进一位
pre = cur
cur = tmp
return pre
原文链接:https://blog.itblood.com/4576.html,转载请注明出处。

![播种我的妻子Seed My Wife 官方中文版+自带全回想 [NTRMAN新作][电脑/480M] [互动SLG/中文/动态]](/wp-content/uploads/replace/2025/10/05/0d31826f9fa2316e368bb167fec1c503.webp)