题解 | #反转链表#

反转链表

http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

1. 递归

import sys
sys.setrecursionlimit(10000)  
# 经测试python3解释器默认递归最大深度为3000,所以当链表长度大于3000的时候,会引发RecursionError异常
class Solution:
    def ReverseList(self , head: ListNode) -> ListNode:
        # write code here
        if head is None or head.next is None:
            return head
        next_ = head.next
        reverse = self.ReverseList(next_)
        next_.next = head
        head.next = None
        return reverse

2.遍历翻转

class Solution:
    def ReverseList(self , head: ListNode) -> ListNode:
        # write code here
        new_head = None
        while head:
            tmp = head.next
            head.next = new_head
            new_head = head
            head = tmp
        return new_head
全部评论

相关推荐

强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-04 14:35
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务