Interview-code-practice-python icon indicating copy to clipboard operation
Interview-code-practice-python copied to clipboard

小问题请教

Open mengruhou opened this issue 5 years ago • 1 comments

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

'''
题目:输入一个链表,从尾到头打印链表每个节点的值
'''

'''
方法一:使用extend,在尾部插入,其实最关键在于[::-1],只不过输入数据多样化,有可能还是集合,所以转成列表
这个方法效率应该还可以,先存入vector,再反转vector
26ms
5512k
'''

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        if not listNode:
            return []

        result = []
        while listNode.next is not None:
            result.extend([listNode.val])
            listNode = listNode.next
        result.extend([listNode.val])

        return result[::-1]

mengruhou avatar Jul 26 '18 09:07 mengruhou