3.オファー] [リンクリスト実装のpythonの端からプリントヘッドを受賞します

タイトル説明

ArrayListの頭に尾から順にリストを返すことによって、リストを入力します。

最初のソリューション

# -*- 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
        answer = []
        head = listNode
        while head:
            answer.append(head.val)
            head = head.next
        return answer[::-1]

第二の溶液

-*- 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
        answer = []
        head = listNode
        while head:
            answer.insert(0, head.val)
            head = head.next
        return answer
公開された99元の記事 ウォンの賞賛6 ビュー4000

おすすめ

転載: blog.csdn.net/weixin_42247922/article/details/103915865