赞
踩
题目描述:
题解一:
class Solution(object): def reverseList(self, head): if head==None or head.next==None: return head pre = None cur = head nxt = cur.next while nxt: cur.next = pre pre = cur cur = nxt nxt = nxt.next cur.next = pre return cur
题解二:递归
1.递归终止条件:输入head为空或head.next为空,返回head
2.递归返回值:反转后的head
3.当前递归:
将head->head.next变为head.next->head
对head.next调用reverseList,得到head.next反转后的结果。
head.next.next变为head
head.next设为空
class Solution(object): def reverseList(self, head): if head==None or head.next==None: return head node = self.reverseList(head.next) head.next.next = head head.next = None return node
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。