LeetCode题解(Offer24):反转链表(Python)

题目:原题链接(简单)

标签:链表

解法时间复杂度空间复杂度执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( 1 ) O(1) O(1)56ms (14.20%)
Ans 2 (Python) O ( N ) O(N) O(N) O ( 1 ) O(1) O(1)44ms (77.44%)
Ans 3 (Python)

解法一:

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if head:
            ans = ListNode(0)
            ans.next = head
            node = head.next
            head.next = None
            while node:
                temp = ans.next
                ans.next = node
                node = node.next
                ans.next.next = temp
            return ans.next

解法二(合并解法一的指针交换):

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if head:
            ans = ListNode(0)
            ans.next, node, head.next = head, head.next, None
            while node:
                ans.next, node.next, node = node, ans.next, node.next
            return ans.next
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页