剑指 Offer II 024. 反转链表

剑指 Offer II 024. 反转链表_反转链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if not head:
            return head
        #初始化一个链表的头
        returnHead = None

        codeHead = head
        id = 0
        while codeHead:
            middleHead = codeHead
            codeHead = codeHead.next
            middleHead.next = returnHead
            returnHead = middleHead
        return returnHead