class ReverseList:
def reverseList1(self,head):
if not head or head.next:
return head
ans=ListNode(-1)
cur=head
while cur:
next=cur.next
cur.next=ans.next
ans.next=cur
cur=next
return ans.next
直接操作链表实现反转(重要)
def reverseList1(self,head):
if not head or not head.next:
return head
cur=head.next
head.next=None
while cur:
next=cur.next
cur.next=head
head=cur
cur=next
return head
拓展:递归实现(完全看不懂-等我学到递归再来看看)
def reverseList1(self,head):
if head is None or head.next is None:
return head
p=self.reverseList(head.next)
head.next.next=head
head.next=None
return p