问题:
输入一个链表,反转链表后,输出新链表的表头。
代码:
# -*- 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 is None:
return None
pre=None
cur=pHead
nxt=pHead.next
while nxt:
cur.next=pre
pre=cur
cur=nxt
nxt=nxt.next
cur.next=pre
return cur
运行时间:27ms
占用内存:5856k