题解 | #从尾到头打印链表#

从尾到头打印链表

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;
    }
}
全部评论

相关推荐

牛马人的牛马人生:500一天吗?香麻了
投递字节跳动等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务