今天爱分享给大家带来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
原文链接:http://blog.itblood.com/4576.html,转载请注明出处。

![我的美熟继母2:家庭派对 官方中文步兵版+自带全CG解放[新作] [放置经营SLG/中文/动态]](/wp-content/uploads/replace/2025/07/18/d6dbe84c00129f770440833ea41df200.webp)