206_反转链表

"""
反转一个单链表。

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


def reverseList(head):

    if head is None:
        return None
    prev = None
    tmp = head
    while head:
        head = head.next
        if head is None:
            break

        tmp.next = prev
        prev = tmp
        tmp = head
    tmp.next = prev
    return tmp



©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页