【leetcode❤python】206. Reverse Linked List

时间:2022-02-24 20:01:54

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def reverseList(self, head):
        if(head==None):return None
        if(head.next==None):return head
       
        # pre=head
        # p=head.next
        # pre.next=None
        # nxt=None
        new_head=None
        while head:
            p=head
            head=head.next
            p.next=new_head
            new_head=p
        return new_head