思路:
利用双指针法来作题:
先定义一个cur将他赋值为头节点,再定义一个pre,因为要反转,反转后的pre刚好指向None,将pre赋值为None即可。
对当前给的链表进行循环遍历:先定义一个temp存储头节点后的第一个节点,最后将pre赋值给cur.next即可,将pre=cur cur=temp目的是将后移,最后开始返回当前的pre值即可
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
'''双指针法'''
class Solution:
def reverList(self,head:ListNode)->ListNode:
cur=head
pre=None
while cur:
temp=cur.next
cur.next=pre
pre=cur
cur=temp
return pre