题解 | #反转链表#

反转链表

https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

class Solution:
    def ReverseList(self , head: ListNode) -> ListNode:
        # 定义一个新的空链表
        res = None
        # 定义一个当前链表存储待处理链表头
        current = head
        # 当前链表不为空进入循环
        while current:
            # 定义一个指向当前链表下一个节点的指针
            temp = current.next
            # 将当前链表的下一个节点指向新的链表头
            current.next = res
            # 反转后的链表
            res = current
            # 当前链表指向下一个节点
            current = temp
        return res
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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