题目描述
输入一个链表,反转链表后,输出新链表的表头。
先用tmp储存head.next,然后将head插入res后面,最后将tmp赋值给head:
# -*- 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
res = ListNode(0)
while pHead:
tmp = pHead.next
pHead.next = res.next
res.next = pHead
pHead = tmp
return res.next