美文网首页
[LeetCode By Python] 206. Revers

[LeetCode By Python] 206. Revers

作者: 乐乐可爱睡觉 | 来源:发表于2016-06-17 16:50 被阅读282次

一、题目

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 By Python] 206. Revers

      本文链接:https://www.haomeiwen.com/subject/nievdttx.html