反转链表 -- 每日一题05

'''
先取出第一个节点,置空next,取名为tail
然后依次取出节点,将取出节点的next指向tail,然后tail调至取出的节点
'''
class Solution:
    def ReverseList(self , head: ListNode) -> ListNode:
        # write code here
        if head == None or head.next==None:
            return head
        tail = head
        # 先将head地址换了
        head = head.next
        tail.next = None
        while head is not None:
            temp = head.next
            head.next = tail
            tail = head
            head = temp
        return tail

全部评论

相关推荐

面了100年面试不知...:太礼貌,还是
点赞 评论 收藏
分享
09-23 14:45
贵州大学 财务
勇敢求职牛牛:怎么9.2佬人手一个中信证券实习
点赞 评论 收藏
分享
评论
点赞
7
分享

创作者周榜

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