定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

    示例:

    输入: 1->2->3->4->5->NULL
    输出: 5->4->3->2->1->NULL

    限制:

    0 <= 节点个数 <= 5000

    1. class Solution:
    2. def reverseList(self, head: ListNode) -> ListNode:
    3. if head is None:
    4. return head
    5. newhead = None
    6. cur = head
    7. nextnode = head.next
    8. while nextnode != None:
    9. cur.next = newhead
    10. newhead = cur
    11. cur = nextnode
    12. nextnode = nextnode.next
    13. cur.next = newhead
    14. newhead = cur
    15. return newhead