题目描述:
输入一个链表,反转链表后,输出新链表的表头。
解题思路:
为防止链表出现断裂的情况,需要定义3个指针,一个指向当前遍历的节点current,以及它的前一个节点previous和后一个节点later
# -*- 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 pHead==None:
return None
current = pHead
previous = None
later = None
while current!=None:
later = current.next
current.next = previous
previous = current
current = later
return previous
网友评论