• #AcWing-从尾到头打印链表


    题目:

    输入一个链表的头结点,按照 从尾到头 的顺序返回节点的值。

    返回的结果用数组存储。

    数据范围

    0≤0≤ 链表长度 ≤1000≤1000。

    样例
    1. 输入:[2, 3, 5]
    2. 返回:[5, 3, 2]

     分析:

    1.错误示范:
    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode(int x) : val(x), next(NULL) {}
    7. * };
    8. */
    9. class Solution {
    10. public:
    11. vector<int> printListReversingly(ListNode* head) {
    12. vector<int>res;
    13. int i=0;/??????????????/
    14. while(head){
    15. res[i++]=head->val;/????????????/
    16. head=head->next;
    17. }
    18. reverse(res.begin(), res.end());
    19. return res;
    20. }
    21. };
    1. res的大小不足:在使用res[i++]=head->val将元素添加到res时,如果i的值超过了res的大小,会导致"Segmentation Fault"错误。可以使用push_back函数将元素添加到res的末尾,而不用手动管理i的值。修改代码如下:

     while (head) {
        res.push_back(head->val);
        head = head->next;
    }

     关于push,push_back,pop,pop_back:
    1. push:该函数是std::stackstd::queue的成员函数,用于将元素添加到容器的顶部(栈顶)或末尾(队尾)。例如:

     std::stack myStack;
    myStack.push(1);  // 将1添加到栈顶

    std::queue myQueue;
    myQueue.push(2);  // 将2添加到队尾
    

     注意:push函数只适用于std::stackstd::queue容器,不能直接用于std::vectorstd::list等容器。

    2.push_back:该函数是std::vectorstd::dequestd::list等容器的成员函数,用于将元素添加到容器的末尾。例如:

     std::vector myVector;
    myVector.push_back(3);  // 将3添加到容器末尾

    std::deque myDeque;
    myDeque.push_back(4);  // 将4添加到容器末尾
     

    std::list myList;
    myList.push_back(5);  // 将5添加到容器末尾
     

     注意:push_back函数只适用于支持在末尾添加元素的容器,如std::vectorstd::dequestd::list等。

     总结:push用于std::stackstd::queue,将元素添加到顶部或末尾;push_back用于std::vectorstd::dequestd::list,将元素添加到末尾。

    1. pop函数用于std::stackstd::queue容器,用于移除容器中的顶部元素(栈顶)或队头元素。例如:

    std::stack myStack;
    myStack.push(1);
    myStack.pop();  // 移除栈顶元素
     

    std::queue myQueue;
    myQueue.push(2);
    myQueue.pop();  // 移除队头元素
     

     注意:pop函数只适用于std::stackstd::queue容器,不能直接用于std::vectorstd::list等容器。

    1. pop_back函数用于std::vectorstd::dequestd::list等容器,用于移除容器中的末尾元素。例如:

    std::vector myVector;
    myVector.push_back(3);
    myVector.pop_back();  // 移除末尾元素
     

     std::deque myDeque;
    myDeque.push_back(4);
    myDeque.pop_back();  // 移除末尾元素

    std::list myList;
    myList.push_back(5);
    myList.pop_back();  // 移除末尾元素
     

     注意:pop_back函数只适用于支持移除末尾元素的容器,如std::vectorstd::dequestd::list等。

    总结:pop用于std::stackstd::queue,移除顶部或队头元素;pop_back用于std::vectorstd::dequestd::list,移除末尾元素。

    代码:

    1. // 法一:reverse 答案数组. 时间:O(n);空间:O(n)
    2. class Solution {
    3. public:
    4. vector<int> printListReversingly(ListNode* head) {
    5. vector<int> res;
    6. while (head) {
    7. res.push_back(head->val);
    8. head = head->next;
    9. }
    10. // reverse(res.begin(), res.end());
    11. // return res;
    12. return vector<int>(res.rbegin(), res.rend());
    13. }
    14. };
    15. // 法二:递归. 时间:O(n);空间:栈空间O(n).
    16. class Solution {
    17. public:
    18. vector<int> printListReversingly(ListNode* head) {
    19. if (!head) return {};
    20. auto res = printListReversingly(head->next);
    21. res.push_back(head->val);
    22. return res;
    23. }
    24. };
    25. // 法三:辅助栈法. 时间:O(n);空间:O(n)
    26. class Solution {
    27. public:
    28. vector<int> printListReversingly(ListNode* head) {
    29. stack<int> s;
    30. while (head) {
    31. s.push(head->val); // 存的是 val
    32. head = head->next;
    33. }
    34. int n = s.size();
    35. vector<int> res(n);
    36. for (int i = 0; i < n; i ++ ) {
    37. res[i] = s.top();
    38. s.pop();
    39. }
    40. return res;
    41. }
    42. };

  • 相关阅读:
    nrf52832 开发板入手笔记:J-Flash 蓝牙协议栈烧写
    977. 有序数组的平方
    浏览器标签上添加icon图标;html引用ico文件
    【Spring】bean的生命周期
    QT-串口工具
    特征提取PCA实现及避坑指南
    用友U8 cloud再升级,为成长型集团注入数智新力量
    02 请求默认值
    Go语言切片
    去中心化数字身份为什么在元宇宙中这么重要
  • 原文地址:https://blog.csdn.net/asdfghrfh/article/details/133197986