题目描述
输入一个链表,反转链表后,输出新链表的表头。

非递归方法AC代码:

def ReverseList(self, pHead):
    # write code here
    prev = None
    while pHead:
        curr = pHead
        pHead = pHead.next
        curr.next = prev
        prev = curr
    return prev

递归方法:

def ReverseList(self, pHead, prev=None):
    if not pHead:
        return prev
    n = pHead.next
    pHead.next = prev
    return self.ReverseList(n, pHead)

还在思考怎么让递归调用方法中函数只传入一个参数。