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

从尾到头打印链表

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

我的思路就是首先将整个链表反转,然后再将链表打印出来。
/**

  • public class ListNode {

  • int val;

  • ListNode next = null;

  • ListNode(int val) {

  • this.val = val;

  • }

  • }

  • /
    import java.util.ArrayList;
    public class Solution {
    public ArrayList<integer> printListFromTailToHead(ListNode listNode) {</integer>

       ListNode pre = null;
       ListNode next = null;
       ArrayList<Integer> list = new ArrayList<Integer>();
       while(listNode != null){
           next = listNode.next;
           listNode.next = pre;
           pre = listNode;
           listNode = next;
       }
    
       while(pre != null){
           list.add(pre.val);
           pre = pre.next;
       }
       return list;

    }
    }

全部评论

相关推荐

xtu大迫杰:偶遇校友,祝校友offer打牌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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