题目描述:
思路:
采用双指针法
代码:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
# 双指针法
pre=None # 反转后的尾结点
cur=head # 初始化
while (cur!=None):
temp=cur.next # 暂存,防治断链,没断之前,先保存
cur.next=pre # 反转
pre=cur # 下面两行代码不能换顺序,因为换了之后,都指向到temp
cur=temp
return pre # 新链表的头结点