leetcode-206 | 反转链表

title: leetcode-206 | 反转链表
date: 2019-5-3 17:37:38
comments: true
categories: “leetcode”
tags:
- leetcode 简单难度
- 链表

题目描述

反转一个单链表。

示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
进阶:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?

解答思路

思想来源于数据结构中的链表的翻转。

翻译如下:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head==None or head.next==None:
            return head
        #加入一个虚拟头节点,方便操作
        virtual = ListNode(0)
        virtual.next = None
        p = head
        q = head.next
        while q:
            p.next = virtual.next
            virtual.next = p
            p = q
            q = q.next
       	#最后一个位置,也就是q,赋值后的p,还没有完成赋值,需要退出循环后再重复一次
        p.next = virtual.next
        virtual.next = p
        return virtual.next

结果:

执行用时 : 44 ms, 在Reverse Linked List的Python提交中击败了36.40% 的用户
内存消耗 : 13.6 MB, 在Reverse Linked List的Python提交中击败了44.93% 的用户

提交时间 状态 执行用时 内存消耗 语言
几秒前 通过 44 ms 13.6MB python

很老土的解法

遍历获取数据,翻转,构成数据格式

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head==None or head.next==None:
            return head
        #可以把数据全部整下来,在反转,在构成
        li = []
        p = head
        while p:
            li.append(p.val)
            p=p.next
        li.reverse()
        head = ListNode(li[0])
        p = head
        for i in range(1,len(li)):
            s = ListNode(li[i])
            p.next = s
            p=p.next
        
        p.next = None
        
        return head

结果:

执行用时 : 44 ms, 在Reverse Linked List的Python提交中击败了36.40% 的用户
内存消耗 : 15.7 MB, 在Reverse Linked List的Python提交中击败了16.62% 的用户

提交时间 状态 执行用时 内存消耗 语言
几秒前 通过 44 ms 15.7MB python

我们使用指针(推荐)

图解:
在这里插入图片描述

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head==None or head.next==None:
            return head
        pre = None
        cur = head
        
        while cur:
            #由于如果cur为None的时候,是没有next的,所以我们将它放置到了循环中,判断非None生效,故而在这里声明我们的饿next指针
            next = cur.next

            #我们的移动逻辑
            cur.next = pre
            pre = cur
            cur = next

        return pre  #cur一定是None

结果:

执行用时 : 36 ms, 在Reverse Linked List的Python提交中击败了100.00% 的用户
内存消耗 : 13.3 MB, 在Reverse Linked List的Python提交中击败了50.21% 的用户

提交时间 状态 执行用时 内存消耗 语言
几秒前 通过 36 ms 13.3MB python

分析:
如果用两个指针,如下图,是不好操作的。
在这里插入图片描述

发布了169 篇原创文章 · 获赞 139 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_26460841/article/details/102573946