题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
# 设置一个链表用于返回接收
res = None
while pHead != None:
# 需要一个结点暂时存放pHead的下一个结点,不然会指针丢失
temp = pHead.next
# 原来的头结点指向返回结果的结点
pHead.next = res
# 都往后挪一位,进行下次循环
res = pHead
pHead = temp
return res
#我的实习求职记录#
