反转链表 -- 每日一题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

全部评论

相关推荐

07-22 11:35
门头沟学院 Java
谁知道这是为什么吗,有没有懂的佬给讲讲
理智的小饼干又熬夜了:鹅打电话问我参不参加后台提前批,说是有的但还没放官网
点赞 评论 收藏
分享
06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
评论
点赞
7
分享

创作者周榜

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