6. The print head from the end of the list

Input a linked list, the print head from the end of the list of values ​​for each node

Class ListNode #: 
# DEF __init __ (Self, Val):
# self.val = Val
# self.next = None

class Solution:
DEF print_from_tail_to_head (Self, ListNode):
# 1. how to represent a linked list? A: You do not specifically define a linked list, only to find a node, follow it to find the next one has been found to tail
# 2. Note the problem is not seeking a linked list, as long as the value of the node to print
RES = []
IF ListNode == none:
return []
elif ListNode.next == none:
return [ListNode.val]
the else:
the while ListNode.next:
res.append (ListNode.val)
ListNode = ListNode.next
res.append (ListNode.val) # Note that no this easy to miss
return res [:: - 1]

Guess you like

Origin www.cnblogs.com/PhilYe/p/11703163.html