输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。
如输入{1,2,3}的链表如下图:
返回一个数组为[3,2,1]
0 <= 链表长度 <= 10000
python解法:
解法1:把链表数据存入列表,之后反转链表;
def printListFromTailToHead(self, listNode):
if not listNode:
return []
res = []
while listNode:
res.append(listNode.val)
listNode = listNode.next
return res[::-1]解法2:递归;
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def __init__(self):
self.res = []
def printListFromTailToHead(self, listNode):
if not listNode:
return self.res
res = self.printListFromTailToHead(listNode.next)
res.append(listNode.val)
return res解法3:逆置链表再存入列表;
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
if not listNode:
return []
p = ListNode(0)
p.next = None
r = p
res = []
while listNode:
q = listNode.next # 暂存后继结点;
listNode.next = p.next
p.next = listNode # 把结点依次插入到自己创建的结点后面,实现逆置;
listNode = q
ptr = r.next
while ptr:
res.append(ptr.val)
ptr = ptr.next
return res解法4:逆置链表再存入列表,和解法3的逆置方法不同;
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
if not listNode:
return []
res = []
p = None
q = listNode
while q:
listNode = listNode.next
q.next = p
p = q
q = listNode
while p:
res.append(p.val)
p = p.next
return res
class Solution: # 返回从尾部到头部的列表值序列,例如[1,2,3] def printListFromTailToHead(self, listNode): # write code here if listNode is None: return [] self.result = [] self.get_after_node(listNode) return self.result def get_after_node(self, listNode): if listNode.next is None: self.result.append(listNode.val) return else: self.get_after_node(listNode.next) self.result.append(listNode.val) return
# -*- 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 ret = [] if listNode == None: return [] while listNode: ret.insert(0, listNode.val) listNode = listNode.next return ret
# -*- 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 l = [] while listNode: l.insert(0,listNode.val) listNode = listNode.next return lappend:只能在列表后方加入新数据;而insert可以在列表任意位置加入新数据。
class Solution: # 返回从尾部到头部的列表值序列,例如[1,2,3] def printListFromTailToHead(self, listNode): (894)# write code here def addElement(listNode,list): if listNode.next != None: addElement(listNode.next, list) list.append(listNode.val) return list list =[ ] if listNode == None: return list else: return addElement(listNode, list)
# -*- 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 L = [] head = listNode while head: L.insert(0,head.val) head = head.next return L
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(struct ListNode* head) { vector<int> value; if(head != NULL) { value.insert(value.begin(),head->val); if(head->next != NULL) { vector<int> tempVec = printListFromTailToHead(head->next); if(tempVec.size()>0) value.insert(value.begin(),tempVec.begin(),tempVec.end()); } } return value; } };/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(struct ListNode* head) { vector<int> value; if(head != NULL) { value.insert(value.begin(),head->val); while(head->next != NULL) { value.insert(value.begin(),head->next->val); head = head->next; } } return value; } };