Skip to content

小问题请教 #1

Description

@mengruhou

第27行,为什么result不能用append添加链表的值?必须用extend吗?

'''题目:输入一个链表,从尾到头打印链表每个节点的值''''''方法一:使用extend,在尾部插入,其实最关键在于[::-1],只不过输入数据多样化,有可能还是集合,所以转成列表这个方法效率应该还可以,先存入vector,再反转vector26ms5512k'''# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = NoneclassSolution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]defprintListFromTailToHead(self, listNode):
# write code hereifnotlistNode:
return []
result= []
whilelistNode.nextisnotNone:
result.extend([listNode.val])
listNode=listNode.nextresult.extend([listNode.val])
returnresult[::-1]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions