我的python第一次记录

我的python第一次记录

first_python_learn
first_python_learn

翻转链表

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

def reverseList(head: ListNode) -> ListNode:
    if not head or not head.next:
        return head
    pre = None
    cur = head
    while cur:
        tmp = cur.next
        cur.next = pre
        pre = cur
        cur = tmp
    return pre
署名 - 非商业性使用 - 禁止演绎 4.0