[LeetCode By Python] 206. Reverse Linked List

一、题目

Reverse Linked List

二、解题

单向列表的倒序。

三、尝试与结果

三个指针:

class Solution(object):
    def reverseList(self, head):
        if head == None:
            return None
        if head.next == None:
            return head
        front = head
        last = None
        mid = None
        while front.next != None:
            mid = front
            front = front.next
            mid.next = last
            last = mid
        front.next = mid
        return front

结果:AC

递归:

class Solution(object):
    def reverseList(self, head):
        if head == None:
            return None
        if head.next == None:
            return head
        last = head.next
        result = self.reverseList(last)
        head.next = None
        last.next = head
        return result

结果:AC

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,349评论 0 33
  • LeetCode 刷题随手记 - 第一部分 前 256 题(非会员),仅算法题,的吐槽 https://leetc...
    蕾娜漢默阅读 18,066评论 2 36
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,035评论 25 709
  • 雷霆翻滚 惊雷四起 万物乍醒 哀愁四处升起 谁的泪划破天际 幻化成雨 一滴一滴砸落大地 他说 快跑 不容置疑 不可...
    繁花千色阅读 1,033评论 0 3
  • 汉 · 佚名 迢迢牵牛星,皎皎河汉女。 纤纤擢素手,札札弄机杼。 终日不成章,泣涕零如雨; 河汉清且浅,相去复几许...
    意象工厂阅读 3,118评论 0 3