讨论/《初级算法》 - 反转链表/
《初级算法》 - 反转链表

emm,这是倒插法的py代码

# 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 head == None:
            return None
        front = head
        rear = head.next
        while rear:
            front.next = rear.next
            rear.next = head
            head = rear
            rear = front.next
        return head
展开全部 38 讨论