题目:输入一个链表,反转链表后,输出链表的所有元素。
代码:
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, head):
# write code here
tmp = None
while head:
cur = head.next
head.next = tmp
tmp = head
head = cur
return tmp