"""
title:面試題06。 從尾到頭列印節點
introduction:
輸入乙個鍊錶的頭節點,從尾到頭反過來返回每個節點的值(用陣列返回)。
示例 1:
輸入:head = [1,3,2]
輸出:[2,3,1]
限制:0 <= 鍊錶長度 <= 10000
"""class listnode:
def __init__(self, x, next=none):
self.val = x
self.next = next
class list:
def __init__(self):
self.head = none
def add(self, val):
if self.head is none:
self.head = listnode(val)
return
p = self.head
while p.next is not none:
p = p.next
p.next = listnode(val)
def printall(self):
p = self.head
while p is not none:
print(p.val, end='')
if p.next is not none:
print(", ", end='')
p = p.next
print('')
class solution:
def reverseprint(self, head):
reslist =
while head:
head = head.next
return reslist[::-1]
if __name__ == "__main__":
a = list()
a.add(3)
a.add(5)
a.add(9)
a.add(13)
a.printall()
b = solution()
x = b.reverseprint(a.head)
print(x)
面試題06 從尾到頭列印鍊錶
輸入乙個鍊錶的頭節點,從尾到頭反過來返回每個節點的值 用陣列返回 示例 1 輸入 head 1,3,2 輸出 2,3,1 限制 0 鍊錶長度 10000 definition for singly linked list.class listnode def init self,x self.val...
leetcode 面試題06 從尾到頭列印鍊錶
面試題06.從尾到頭列印鍊錶 難度簡單27 輸入乙個鍊錶的頭節點,從尾到頭反過來返回每個節點的值 用陣列返回 示例 1 輸入 head 1,3,2 輸出 2,3,1 definition for singly linked list.class listnode def init self,x se...
劍指offer面試題06 從尾到頭列印鍊錶
遍歷鍊錶到陣列 然後反轉後返回即可 date 2020 03 10 22 00 param param head return return int author wwh description 2020年03月10日21 50 33 2020年03月10日22 00 48 面試題06.從尾到頭列印...