这道题之前做过,但是忘的一干二净,这里再写一遍。
206.反转链表
92.反转链表Ⅱ
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
if not pHead or not pHead.next:return pHead
pre, cur, reversedHead = None, pHead, None
while cur != None:
nxt = cur.next
if nxt == None:
reversedHead = cur
cur.next = pre
pre = cur
cur = nxt
return reversedHead
考点
扩展
用递归实现同样的反转链表的功能。