思路
三个指针,直接看代码更好理解。注意最后返回的是prevN,而不是pHead,因为最终的pHead是为None的
AC代码
# -*- 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
if not pHead:
return None
prevN=None
nextN=None
while pHead:
nextN=pHead.next
pHead.next=prevN
prevN=pHead
pHead=nextN
return prevN