# -*- 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
'''
pre=None
order=None
while(pHead!=None):
order=pHead.next
pHead.next=pre
pre=pHead
pHead=order
return pre
'''
pre=None
order=None
while pHead!=None:
order=pHead.next
pHead.next=pre
pre=pHead
pHead=order
return pre
思考反转链表中的每一步操作
用pre和order两个指针分别代表当前指针的前面和后面,当pHead=1时,1的前面是None,后面是1,反转的话要记录None和1,反转后会丢失2节点,首先记录order=pHead.next=2,将pHead.next指向pre=None。更新pre=当前的pHead,再更新pHead指向下一个节点order