题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
class Solution:
def ReverseList(self , pHead):
if pHead == None or pHead.next == None: #若头节点为空则返回
return pHead
leftpoint = pHead #定义三个空节点,left指向头节点
midpoint = leftpoint.next #mid指向头节点的下一个节点
rightpoint = midpoint.next #right指向mid的下一个节点
leftpoint.next = None #将头节点指向空节点
while rightpoint != None:
midpoint.next = leftpoint #将所有节点后移一位
leftpoint = midpoint
midpoint = rightpoint
rightpoint = rightpoint.next
midpoint.next = leftpoint return midpoint #mid为反转后的表头