python 剑指offer系列:反转链表

来源:互联网 发布:淘宝店刷信誉可靠吗 编辑:程序博客网 时间:2024/05/21 12:50

题目:输入一个链表,反转链表后,输出链表的所有元素。

代码:

# -*- coding:utf-8 -*-# class ListNode:#     def __init__(self, x):#         self.val = x#         self.next = Noneclass 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