反转链表
反转链表_牛客网
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述
输入一个链表,反转链表后,输出新链表的表头。
非递归方法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)
还在思考怎么让递归调用方法中函数只传入一个参数。