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

从尾到头打印链表

http://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035

func printListFromTailToHead ( _ head: ListNode?) -> [Int] {
// write code here

    var listArr:[Int] = [];
    var tmp:ListNode? = head;
    while (tmp != nil){
        listArr.insert(tmp!.val, at: 0);
        tmp = tmp!.next;
    }
   return listArr;

}
两个点:1.判断好边界条件 2.遍历链表时每次插入组头,
why:swift耗时要这么久?

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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