题解 | #从尾到头打印链表#
从尾到头打印链表
http://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
using System.Collections.Generic;
/*
public class ListNode
{
public int val;
public ListNode next;
public ListNode (int x)
{
val = x;
}
}*/
class Solution {
// 返回从尾到头的列表值序列
public List<int> printListFromTailToHead(ListNode listNode) {
// write code here
List<int> list = new List<int>();
List<int> list2 = new List<int>();
ListNode head = listNode;
for(int i=0;head!=null;i++){
list.Add(head.val);
head = head.next;
}
// int length = list.length;
// for(int i=0; i<list.length;i++){
// list2[i] = list[length-i];
// }
list.Reverse();
return list;
}
}
/*
public class ListNode
{
public int val;
public ListNode next;
public ListNode (int x)
{
val = x;
}
}*/
class Solution {
// 返回从尾到头的列表值序列
public List<int> printListFromTailToHead(ListNode listNode) {
// write code here
List<int> list = new List<int>();
List<int> list2 = new List<int>();
ListNode head = listNode;
for(int i=0;head!=null;i++){
list.Add(head.val);
head = head.next;
}
// int length = list.length;
// for(int i=0; i<list.length;i++){
// list2[i] = list[length-i];
// }
list.Reverse();
return list;
}
}

