关于数据结构与算法:每日leetcode反转链表

题目

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

输出:head = [1,2,3,4,5]
输入:[5,4,3,2,1]

题解

考查链表构造的了解,递归办法的实现

def reverseList(head):
    if not head or not head.next:
        return head
    
    curHead = reverseList(head.next)
    head.next.next = head
    head.next = None
    return curHead

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理