zoukankan      html  css  js  c++  java
  • 3. 从尾到头打印链表

     题目:

    思路:

    方法1: java实现  性能最差

    import java.util.Collections;
    import java.util.ArrayList;
    public class Solution {
        public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
            //node *p = next;
            ArrayList<Integer> List = new ArrayList<Integer>();
            while(listNode != null){
                List.add(listNode.val);
                listNode= listNode.next;
            }
        Collections.reverse(List); //反转链表
        return List;
        }
    }

    方法2: java递归

    public class Solution {
        ArrayList<Integer> arrayList=new ArrayList<Integer>();
        public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
            if(listNode!=null){
                this.printListFromTailToHead(listNode.next);
                arrayList.add(listNode.val);
            }
            return arrayList;
        }
    }

    方法3: c++ 链表从尾到头输出,利用递归实现,不使用库函数直接printf输出: 性能最优

    /**
    *  struct ListNode {
    *        int val;
    *        struct ListNode *next;
    *        ListNode(int x) :
    *              val(x), next(NULL) {
    *        }
    *  };
    */
    class Solution {
    public:
        vector<int> printListFromTailToHead(struct ListNode* head) {
            vector<int> value; //使用向量存储
            if(head != NULL)
            {
                value.insert(value.begin(),head->val);//将val插入value头部;
                if(head->next != NULL)
                {
                    vector<int> tempVec = printListFromTailToHead(head->next); //递归调用赋值给tempvec
                    if(tempVec.size()>0)
                    value.insert(value.begin(),tempVec.begin(),tempVec.end());//将tempvec插入value中 (每次插入头,不用反转)
                }         
                 
            }
            return value;
        }
    };

    方法4 :c++ 用库函数,每次扫描一个节点,将该结点数据存入vector中,如果该节点有下一节点,将下一节点数据直接插入vector最前面,直至遍历完,或者直接加在最后,最后调用reverse

    /**
    *  struct ListNode {
    *        int val;
    *        struct ListNode *next;
    *        ListNode(int x) :
    *              val(x), next(NULL) {
    *        }
    *  };
    */
    class Solution {
    public:
        vector<int> printListFromTailToHead(struct ListNode* head) {
            vector<int> value;
            if(head != NULL)
            {
                value.insert(value.begin(),head->val);
                while(head->next != NULL)
                {
                    value.insert(value.begin(),head->next->val);
                    head = head->next;
                }         
                 
            }
            return value;
        }
    };
  • 相关阅读:
    Beginning Python From Novice To Professional读书笔记
    Google's Python Class
    Screen scraping 3
    Screen scraping 1
    Screen scraping 2
    《发现你的销售力量》读书笔记
    不可思议的每日培训
    “项目计划与跟踪最佳实践”讲座(2010年7月)现接受企业申请
    “活用类图,把握需求主动权”讲座(2010年6月)现接受企业申请
    项目健康状况检查
  • 原文地址:https://www.cnblogs.com/msymm/p/9930620.html
Copyright © 2011-2022 走看看