zoukankan      html  css  js  c++  java
  • 剑指offer:面试题5、从尾到头打印链表

    题目描述

    输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

    代码示例

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Stack;
    
    public class Offer5 {
        public static void main(String[] agrs) {
            //构建链表
            ListNode head = new ListNode(1);
            head.next = new ListNode(2);
            head.next.next = new ListNode(3);
            Offer5 testObj = new Offer5();
            //打印列表
            testObj.printList(head);
            //测试法1
    //        List<Integer> res = testObj.printListFromTailToHead(head);
            //测试法2
    //        List<Integer> res = testObj.printListFromTailToHead2(head);
            //测试法3
            List<Integer> res = testObj.printListFromTailToHead3(head);
            System.out.println(res);
        }
    
        //法1:最容易想到的使用栈
        public List<Integer> printListFromTailToHead(ListNode listNode) {
            Stack<Integer> stack = new Stack<>();
            while (listNode != null) {
                stack.add(listNode.val);
                listNode = listNode.next;
            }
            List<Integer> res = new ArrayList<>();
            while (!stack.isEmpty()) {
                res.add(stack.pop());
            }
            return res;
        }
    
        //法2:头插法逆序链表
        public List<Integer> printListFromTailToHead2(ListNode listNode) {
            ListNode head = new ListNode(-1);
            while (listNode != null) {
                ListNode next = listNode.next;
                listNode.next = head.next;
                head.next = listNode;
                listNode = next;
            }
            List<Integer> res = new ArrayList<>();
            head = head.next;
            while (head != null) {
                res.add(head.val);
                head = head.next;
            }
            return res;
        }
    
        //法3:使用递归
        public List<Integer> printListFromTailToHead3(ListNode listNode) {
            List<Integer> res = new ArrayList<>();
            if (listNode != null) {
                res.addAll(printListFromTailToHead3(listNode.next));
                res.add(listNode.val);
            }
            return res;
        }
    
        private void printList(ListNode head) {
            if (head == null) {
                return;
            }
            while (head != null) {
                System.out.println(head.val);
                head = head.next;
            }
        }
    
        static class ListNode {
            int val;
            ListNode next;
            ListNode(int val) {
                this.val = val;
            }
        }
    }
    
    
  • 相关阅读:
    C++强大的背后
    C++ 非托管的vc工程中部分文件使用.Net Framwork
    C++ 基于 Visual C++6.0 的 DLL 编程实现
    C++ 中指针,指针的引用,指针的指针的区别
    C# 中重用c/c++旧模块
    C++ 打开exe文件的方法(VS2008)
    C++ 指针 指针高级<高质量编程>
    C++ 函数指针
    有用但不常见的c++函数
    C++ int & *p; //不能建立指向引用的指针;int *a; int * & p=a; //正确,指针变量的引用
  • 原文地址:https://www.cnblogs.com/ITxiaolei/p/13138681.html
Copyright © 2011-2022 走看看