题目描述
输入一个链表,反转链表后,输出新链表的表头。
时间限制:1秒;空间限制:32768K;本题知识点:链表
解题思路
# -*- 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 or pHead.next==None:
return pHead
pre = None
cur = pHead
while cur:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre