-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path从尾到头打印链表.py
63 lines (56 loc) · 1.62 KB
/
从尾到头打印链表.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''
输入一个链表,按链表值从尾到头的顺序返回一个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
if not listNode:
return []
result =[]
while listNode:
result.insert(0, listNode.val)
listNode = listNode.next
return result
# -*- 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 []
arraylist = []
while listNode:
arraylist.append(listNode.val)
listNode = listNode.next
arraylist.reverse()
return 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
if not listNode:
return []
arraylist = []
newlist = []
while listNode:
arraylist.append(listNode.val)
listNode = listNode.next
while arraylist:
newlist.append(arraylist.pop())
return newlist