【链表专题】剑指 Offer 06. 从尾到头打印链表

本问题对应课程链接:剑指offer刷题第四期

要求:

1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 64 个回答

  • mpweixin用户 普通 2023年8月1日 下午11:20

    打卡

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    private:
        vector<int> a;
    public:
        vector<int> reversePrint(ListNode* head) {
            if(head!=nullptr){
                reversePrint(head->next);
                a.push_back(head->val);
            }
            return a;
        }
    };
    
    0 赞同 0 条回复
  • 今天没时间刷题 普通 2023年7月18日 下午11:14
    空间O(N), 时间O(N)
    class Solution {
    public:
        vector<int> reversePrint(ListNode* head) {
            vector<int> temp;
            if(head == nullptr)
            {
                return {};
            }
            temp = reversePrint(head->next);
            temp.push_back(head->val);
            return temp;
        }
    };
    
    0 赞同 0 条回复
  • mpweixin用户 永久会员 2023年7月18日 上午11:49
    class Solution {
        public int[] reversePrint(ListNode head) {
            //int size = 0;
            //return reverse(head, size);
            return reverse(head, 0);
        }
    
        private int[] reverse(ListNode head, int size){
            if(head == null){
                //int[] result = new int[size];
                //return result;
                return new int[size];
            }
            int[] result = reverse(head.next, size + 1);
            result[result.length - 1 - size] = head.val;
            return result;
        }
    }
    
    • 时间复杂度O(N)
    0 赞同 0 条回复
  • 始料未及 普通 2023年7月17日 下午10:42

    栈:将链表元素压入栈中,获取站的长度,定义数组,依次取出栈中元素;通过Stack实现
    递归:链表先走到头,判断当前节点为空时,获得元素个数,new数组;开始返回,数组添加元素,每返回一层index++,用于构建下标
    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    // class Solution {
    // public int[] reversePrint(ListNode head) {
    // Stack stack=new Stack();
    // ListNode cur=head;
    // while(cur!=null){
    // stack.push(cur);
    // cur=cur.next;
    // }
    // int size=stack.size();
    // int[] result=new int[size];
    // for(int i=0;i<size;i++){
    // result[i]=stack.pop().val;
    // }
    // return result;
    // }
    // }

    class Solution {
    int[] arr=null;
    int length=0;
    int index=0;
    public int[] reversePrint(ListNode head) {
    reverse(head);

       return arr;
    }
    void reverse(ListNode head){
        if(head==null){
            arr=new int[length];
            return;
        }
        length++;
        reverse(head.next);
        arr[index]=head.val;
        index++;
    }
    

    }

    0 赞同 0 条回复
  • hi 普通 2023年7月16日 上午10:46

    先记录链表值,再逆序数组

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> reversePrint(ListNode* head) {
            vector<int> res;
    
            while(head)
            {
                res.push_back(head -> val);
                head = head -> next;
            }
    
            return vector<int>(res.rbegin(), res.rend());
        }
    };
    
    0 赞同 0 条回复
  • wowo 普通 2023年7月14日 上午8:30

    递归

    class Solution:
        def reversePrint(self, head: ListNode) -> List[int]:
            if head:
                return self.reversePrint(head.next) + [head.val]
            else:
                return []
    
    0 赞同 0 条回复
  • momo 普通 2023年7月12日 上午9:47

    时间复杂度O(n)
    空间复杂度O(n)

    class Solution {
        public int[] reversePrint(ListNode head) {
            Deque<Integer> stack = new ArrayDeque<>();
            while (head != null) {
                stack.push(head.val);
                head = head.next;
            }
            int[] res = new int[stack.size()];
            int i = 0;
            while (!stack.isEmpty()) {
                res[i++] = stack.pop();
            }
            return res;
        }
    }
    
    0 赞同 0 条回复
  • 望仔 普通 2023年7月11日 下午11:58
       func reversePrint(head *ListNode) []int {
        /*
            直接用栈很简单
            试用递归,出口:遍历到nil即为终点
            其他的就是往每一层上面记录数字
        */
    
        if head == nil {
            return nil
        }
    
        return append(reversePrint(head.Next), head.Val)
    }
    
    0 赞同 0 条回复
  • Edenms-u 普通 2023年7月11日 下午11:56
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    void return_lenth(struct ListNode* head, int* returnSize){
        int n = 0;
        while(head){
            head=head->next;
            n++;
        }
        *returnSize = n;
    }
    
    int* Print(struct ListNode* head, int* ar){
        if(head->next != NULL){
            ar = Print(head->next, ar);
        }
        *ar = head->val;
        return ++ar;
    }
    
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* reversePrint(struct ListNode* head, int* returnSize){
        if(!head){
            *returnSize = 0;
            return NULL;
        }
        return_lenth(head, returnSize);
        int *ar = (int*)malloc(sizeof(int)*(*returnSize));
        Print(head, ar);
        /*
        int n = *returnSize - 1;
    
        for(int i = n; i >= 0; i--){
            ar[i] = head->val;
            head = head->next;
        }
        */
        return ar;
    }
    

    递归+循环

    0 赞同 0 条回复
  • Rico 普通 2023年7月11日 下午7:25
    class Solution {
    public:
        vector<int> reversePrint(ListNode* head) {
            if(!head)
                return {};
    //递归
            vector<int> a=reversePrint(head->next);
            a.push_back(head->val);
            return a;
        }
    };
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月11日 下午5:06
    //打卡
    class Solution {
    public:
        vector<int> reversePrint(ListNode* head) {
            vector<int> res;
    
            helper(head,res);
    
            return res;
        }
        void helper(ListNode* head,vector<int> &res){
            if(head == nullptr){
                // res.push_back(head->val);
                return;
            }
            helper(head->next,res);
            res.push_back(head->val);
        }
    };
    
    0 赞同 0 条回复
  • 我家旺仔 普通 2023年7月11日 下午4:35

    打卡:
    class Solution {
    public:
    vector reverse;
    vector
    reversePrint(ListNode* head) {

        if(head){
            reversePrint(head->next);
            reverse.push_back(head->val);
        }
        return reverse;
    }
    

    };

    0 赞同 0 条回复
  • 达浪达浪 普通 2023年7月11日 下午12:17
    添加一个递归函数
    class Solution {
    private:
        vector<int> ans;
    public:
        void dfs(ListNode* node){
            if(node==nullptr) return;
            dfs(node->next);
            ans.push_back(node->val);
        }
        vector<int> reversePrint(ListNode* head) {
            dfs(head);
            return ans;
        }
    };
    
    0 赞同 0 条回复
  • NullPointerException 普通 2023年7月11日 上午10:50

    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public int[] reversePrint(ListNode head) {
    //三种 一个用栈 一个用遍历头差法 递归
    ListNode pre =new ListNode();
    pre.next=null;
    ListNode temp;
    int n =0;
    while(head!=null)
    {
    temp=head;
    head=head.next;
    temp.next =null;
    temp.next=pre.next;
    pre.next=temp;
    n++;
    }
    int result[]=new int[n];
    n=0;
    temp=pre.next;
    while(temp!=null)
    {
    result[n++]=temp.val;
    temp=temp.next;
    }
    return result;
    }
    }

    0 赞同 0 条回复
  • 周小二 普通 2023年7月10日 下午8:30
    class Solution {
        List<Integer> tmpList = new ArrayList<>();
        public int[] reversePrint(ListNode head) {
            reverse(head);
            int[] revertArray = new int[tmpList.size()];
            for(int i = 0; i<tmpList.size(); i++) {
                revertArray[i] = tmpList.get(i);
            }
            return revertArray;
        }
    
        //递归获取反向链表
        private void reverse(ListNode head) {
            if (head == null) {
                return;
            }
            reverse(head.next);
            tmpList.add(head.val);
        }
    }
    
    0 赞同 0 条回复
  • w 普通 2023年7月10日 下午1:49
    class Solution {
        public int[] reversePrint(ListNode head) {
            if(head == null){
                return new int[0];
            }
            // 统计链表节点个数,方便创建数组
            int count = 0;
            ListNode temp = head;
            while(temp != null){
                count++;
                temp = temp.next;
            }
            int[] res = new int[count];
            int k = count - 1;
            // 从右往左填充数组
            while(head != null){
                res[k--] = head.val;
                head = head.next;
            }
    
            return res;
        }
    }
    
    0 赞同 0 条回复
  • 富城 普通 2023年7月10日 下午1:04

    代码如下

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int[] reversePrint(ListNode head) {
            int i= 0; 
    
            ListNode  pre=head;
            if(head ==null){
                return new int[0];
            } 
    
            while(pre !=null)
            {
                pre=pre.next;
                i++;
            }
            int[] mergy = new int[i];
            for(int j=i;j>0;j--)
            {            
                mergy[j-1]=head.val;
                head=head.next;
            }
            return mergy;
    
    
        }
    }
    
    0 赞同 0 条回复
  • 坏掉的世界 永久会员 2023年7月10日 上午10:07

    代码

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> res;
        vector<int> reversePrint(ListNode* head) {
            if(!head)
                return res;
            reversePrint(head -> next);
            res.push_back(head -> val);
            return res;
        }
    };
    
    0 赞同 0 条回复
  • offer收割机 普通 2023年7月10日 上午10:03

    从尾到头打印练笔哦

    递归法

    class Solution {
        ArrayList<Integer> tmp = new ArrayList<Integer>();
        public int[] reversePrint(ListNode head) {
            recur(head);
            int[] res = new int[tmp.size()];
            for(int i = 0; i < res.length; i++)
                res[i] = tmp.get(i);
            return res;
        }
        void recur(ListNode head) {
            if(head == null) return;
            recur(head.next);
            tmp.add(head.val);
        }
    }
    
    0 赞同 0 条回复
  • V2ero 普通 2023年7月10日 上午9:59

    代码如下

    function reversePrint(head: ListNode | null): number[] {
        const res = [];
    
        const dfs = (head: ListNode | null) => {
            if(!head){
                return
            }
            dfs(head.next)
            res.push(head.val)
        }
    
        dfs(head)
    
        return res;
    };
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月9日 下午11:29

    “`java
    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public int[] reversePrint(ListNode head) {
    if(head == null)
    return new int[0];//链表为空返回空数组

        int count = 0;//用于计数
        ListNode temp = head;
        while(temp != null){
            count++;
            temp = temp.next;
        }
    
        int[] result = new int[count];//new数组装反转链表数据
        int i = count - 1;
        while(head != null){
            result[i--] = head.val;
            head = head.next;
        }
    
        return result;
    }
    

    }

    0 赞同 0 条回复
  • @ 李小二 普通 2023年7月9日 下午10:45

    public int[] reversePrint(ListNode head) {
    if (head null) {
    return new int[0]; // 如果链表为空,返回一个空数组
    }

        // 统计链表节点个数,方便创建数组
        int count = 0;
    
        ListNode temp = head;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
    
        int[] res = new int[count]; // 创建一个与节点个数相同大小的数组
        int k = count - 1;
        while (head != null) {
            res[k--] = head.val; // 将节点值倒序存储到数组中
            head = head.next;
        }
    
        return res; // 返回倒序存储节点值的数组
    }
    
    0 赞同 0 条回复
  • 爱吃胡萝卜🥕的兔子🐰 普通 2023年7月9日 下午9:29

    打卡

    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func reversePrint(head *ListNode) []int {
        if head == nil{
            return []int{}
        }
    
        nodes := reversePrint(head.Next)
        nodes = append(nodes, head.Val)
        return nodes
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月9日 下午8:34

    打卡:
    class linkelist:

    def __init__(self, data=None):
        self.data = data
        self.head = None
        self.next = None
    
    def reverse(self, head):
        if not head:
            return []
    
        count = 0
        temp = head
        while temp:
            count +=1
            temp = temp.next
    
        lis = [0]*count
        k = count-1
    
        while head:
            lis[k] = head.val
            k -=1
            head = head.next
    
        return lis
    
    0 赞同 0 条回复
  • KCOcean 普通 2023年7月9日 下午7:00

    代码

        vector<int> reversePrint(ListNode* head) {
            if(!head) return {};
            ListNode *temp = head;
            int count = 0,k;
            while(temp){
                count++;
                temp = temp->next;
            }
            k = count - 1;
            vector<int> res(count);
            while(head){
                res[k--] = head->val;
                head = head->next;
            }
            return res;
        }
    
    0 赞同 0 条回复
  • ㅤ 普通 2023年7月9日 下午6:31

    时间复杂度:O(n)
    空间复杂度:O(n)

    /**
         * 剑指 Offer 06. 从尾到头打印链表
         * https://leetcode.cn/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
         * 思路:遍历链表获取长度,从最大的索引开始向下存储链表元素
         */
        public int[] reversePrint(ListNode head) {
            ListNode tempNode = head;
            // 1、先遍历 ListNode 获取链表长度 n
            int n = 0; // 数组长度
            while (tempNode != null) {
                n++;
                tempNode = tempNode.next;
            }
            // 2、根据长度 n 来创建数组,重新遍历链表,同时按倒叙插入数组
            tempNode = head;
            int[] ans = new int[n];
            while (n > 0) {
                ans[--n] = tempNode.val;
                tempNode = tempNode.next;
            }
            return ans;
        }
    
    0 赞同 0 条回复
  • 神经蛙 普通 2023年7月9日 下午6:24

    时间复杂度:O(n)
    空间复杂度:O(n)

    class Solution {
        // 定义变量,不用通过list转数组
        int length = 0;
        int i = 0;
        int [] res;
        public int[] reversePrint(ListNode head) {
            reverse(head);
            return res;
        }
    
        private void reverse(ListNode node) {
            // 递归结束,初始化数组
            if (node == null) {
                res = new int[length];
                return;
            }
    
            // 数组长度++
            ++length;
            // 递归处理数组
            reverse(node.next);
            // 将值放入数组
            res[i++] = node.val;
        }
    }
    
    0 赞同 0 条回复
  • 艺 普通 2023年7月9日 下午6:09

    时间复杂度:O(n)
    空间复杂度:O(1)

    “`c++
    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode *next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    vector reversePrint(ListNode* head) {
    ListNode *p;
    p=head;
    int count=0;
    while(p!=NULL)
    {
    count++;
    p=p->next;
    }
    vector
    a(count);
    int i;
    p=head;
    for(i=count-1;i>=0;i–)
    {
    a[i]=p->val;
    p=p->next;
    }
    return a;
    }
    };
    “`

    0 赞同 0 条回复
  • Mooby 普通 2023年7月9日 下午5:56

    二
    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public int[] reversePrint(ListNode head) {
    if (head null) {
    return new int[0];
    }
    int count = 0;
    ListNode temp = head;
    while (temp != null) {
    count++;
    temp = temp.next;
    }
    int[] res = new int[count];
    int k = count – 1;
    while (head != null) {
    res[k–] = head.val;
    head = head.next;
    }
    return res;
    }
    }

    0 赞同 0 条回复
  • ppopp 普通 2023年7月9日 下午3:25

    ”’cpp
    //栈模拟递归
    class Solution {
    public:
    vector reversePrint(ListNode* head){
    stack
    stk;
    vector
    ans;

        //将链表节点数值入栈
        while (head != nullptr){
           stk.push(head -> val);
           head = head -> next; 
        }
    
        //出栈的时候将节点加入返回的答案队列
        while(stk.size()){
            auto x = stk.top();
            stk.pop();
    
            ans.push_back(x);
        }
    
        return ans;
    }
    

    };
    ”’

    0 赞同 0 条回复
  • finalrgy 普通 2023年7月9日 下午3:19
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func reversePrint(head *ListNode) []int {
        if head == nil {
            return []int{}
        }
        res := reversePrint(head.Next)
        res = append(res, head.Val)
        return res
    }
    
    1. 递归遍历节点,向上回溯的时候把当前节点的值加上
    2. 时间复杂度O(N),空间复杂度O(N)
    0 赞同 0 条回复
  • 3A87 普通 2023年7月9日 下午3:16
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> reversePrint(ListNode* head) {
            if(!head) return {};
            ListNode* temp=head;
            int cnt;
            while(temp)
            {
                cnt++;
                temp=temp->next;
            }
            vector<int> a(cnt);
            int k=cnt-1;
            //从右往左填充数组
            while(head)
            {
                a[k--]=head->val;
                head=head->next;
            } 
            return a;
        }
    };
    
    0 赞同 0 条回复
  • 刹那芳间 普通 2023年7月9日 下午2:53
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int[] reversePrint(ListNode head) {
            ListNode cur = head; //用来遍历的节点
            //计算链表长度
            int size = 0;
            while(cur != null){
                size++;
                cur = cur.next;
            }
    
            //建一个数组用来存储返回来遍历的节点
            int[] res = new int[size];
    
            cur = head;
            int n = size-1;
            //单链表遍历只能从前往后,所以从数组后面赋值
            while(cur != null){
                res[n--] = cur.val;
                cur = cur.next;
            }
    
            //返回数组
            return res;
    
        }
    }
    
    0 赞同 0 条回复
  • Evilluu 普通 2023年7月9日 下午1:51

    class Solution:
    def reversePrint(self, head: ListNode) -> List[int]:
    list=[]
    while head:
    list.append(head.val)
    head=head.next
    return list[::-1]
    列表倒序输出

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月9日 下午1:40

    用stack和cpp vector特性

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
      vector<int> reversePrint(ListNode *head) {
        vector<int> result;
        while (head != NULL) {
          result.insert(result.begin(), head->val);
          head = head->next;
        }
        return result;
      }
    };
    
    0 赞同 0 条回复
  • AD钙奶 普通 2023年7月9日 上午11:54

    思路如下:
    1. 先遍历,统计链表长度size
    2. 创建长度为size的数组
    3. 将链表元素从尾到头存入,由于单链表元素不能从尾至头遍历,则考虑将单链表从头开始遍历,然后数组从尾至头存储元素。

    class Solution {
        public int[] reversePrint(ListNode head) {
            ListNode cur = head;
            int size = 0;
            while(cur != null) {
                size ++;    
                cur = cur.next;
            } 
            int[] res = new int[size];
            cur = head;
            int n = size - 1;
            while(cur != null) {
                res[n --] = cur.val;
                cur = cur.next;
            }
            return res;
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(1)

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月9日 上午11:03

    class Solution {
    public int[] reversePrint(ListNode head) {
    if(head null) return new int [0];
    ListNode p = head;
    int count= 0;
    while(p!=null){
    count++;
    p=p.next;
    }
    int[] x = new int[count];
    for(int i=count-1;i>=0;i–){
    x[i]=head.val;
    head=head.next;
    }
    return x;
    }
    }
    时间复杂度 O(n)
    空间复杂度O(n)

    0 赞同 0 条回复
  • 枫梓。 普通 2023年7月9日 上午9:07
    class Solution {
    public:
        vector<int> reversePrint(ListNode* head) {
            ListNode *tmp = head;
            int n = 0;
            while(tmp) {
                ++n;
                tmp = tmp->next;
            }
            vector<int> p(n);
            tmp = head;
            for(int i = 0; i < n; ++i) {
                p[n - 1 - i] = tmp->val;
                tmp = tmp->next;
            }
            return p;
        }
    };
    

    从尾到头打印链表,第一遍遍历链表获得长度以创建数组,第二遍存入数组。 时间O(n) 空间O(n)

    0 赞同 0 条回复
  • 新禾 普通 2023年7月9日 上午1:20

    利用栈
    时间复杂度:on
    空间复杂度:on
    class Solution {
    public int[] reversePrint(ListNode head) {
    Stack stack = new Stack();
    ListNode t = head;
    while(t != null) {
    stack.push(t);
    t = t.next;
    }
    int size = stack.size();
    int[] res = new int[size];
    for (int i = 0; i < size; i++) {
    res[i] = stack.pop().val;
    }
    return res;
    }
    }

    0 赞同 0 条回复
  • 片言莫语 普通 2023年7月9日 上午12:17
    class Solution {
        // 主要思路是从左向右遍历链表元素,但是从右向左存到一个新数组中
        public int[] reversePrint(ListNode head) {
            if(head == null){
                return new int[0];
            }
            int count = 0;  //方便统计数组元素的个数
            ListNode cur = head;
    
            while(cur != null){ //一直移动到链表的最后一个结点
                count++;
                cur = cur.next;
            }
            int[] res = new int[count]; //建立新数组存结果
            int k = count - 1;  //k是新数组的指针
            while(head!=null){
                res[k--] = head.val;    //将链表的第一个数(最左边的)存在新数组的最右边
                head = head.next;   //链表的头结点继续向下移动
            }
            return res;
        }
    }
    
    0 赞同 0 条回复
  • 人生何处不青山 普通 2023年7月8日 下午11:42

    简单题刷起来还是挺爽的

    /**
     * Definition for singly-linked list.
     * class ListNode {
     *     val: number
     *     next: ListNode | null
     *     constructor(val?: number, next?: ListNode | null) {
     *         this.val = (val===undefined ? 0 : val)
     *         this.next = (next===undefined ? null : next)
     *     }
     * }
     */
    
    function reversePrint(head: ListNode | null): number[] {
        let res:number[] = [];
        function dfs(root:ListNode | null) {
            if(!root) return;
            dfs(root.next);
            res.push(root.val);
        }
        dfs(head);
        return res;
    };
    
    0 赞同 0 条回复
  • June 普通 2023年7月8日 下午10:39
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int[] reversePrint(ListNode head) {
            if (head == null)
            {
                int [] nullAns = {};
                return nullAns;
            }
            List<Integer> arr = new ArrayList<>();
            arr = solve(head);
            int [] ans = new int[arr.size()];
            for(int i=0;i<arr.size();i++)
                ans[i] = arr.get(i);
            return ans;
        }
    
        public List<Integer> solve(ListNode node)
        {
            if(node.next == null)
            {
                List<Integer> tmp = new ArrayList<>();
                tmp.add(node.val);
                return tmp;
            }
            List<Integer> tmp = solve(node.next);
            tmp.add(node.val);
            return tmp;
        }
    }//递归方法
    
    0 赞同 0 条回复
  • Fighting boy😀 普通 2023年7月8日 下午10:23

    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    // 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)
    class Solution {
    public:
    vector reversePrint(ListNode
    head) {
    if (!head) {
    return {};
    }

        int count = 0;
        ListNode* temp = head;
        while(temp) {
            count++;
            temp = temp->next;
        }
    
        vector<int> res(count);
        int k = count - 1;
        while (head) {
            res[k--] = head->val;
            head = head->next;
        }
        return res;
    }
    

    };

    0 赞同 0 条回复
  • CKX 普通 2023年7月8日 下午10:15
    func reversePrint(head *ListNode) []int {
        //递归终止条件
        if head == nil {
            return nil
        }
        //进行递归
        return append(reversePrint(head.Next), head.Val)
    }
    
    • 时间O(n)
    • 空间O(n)
    0 赞同 0 条回复
  • #00456B 普通 2023年7月8日 下午10:02

    先写一个reverse函数,用双指针来进行链表的反转。然后将反转后的链表按顺序输出。

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* reverse(ListNode* head){
            ListNode* pre = NULL;
            ListNode* cur = head;
            ListNode* temp;
            while(cur){
                temp = cur->next;
                cur->next = pre;
                pre = cur;
                cur = temp;
            }
            return pre;
        }
        vector<int> reversePrint(ListNode* head) {
            head = reverse(head);
            vector<int> ans;
            while(head){
                ans.push_back(head->val);
                head = head->next;
            }
            return ans;
        }
    };
    
    0 赞同 0 条回复
  •  普通 2023年7月8日 下午9:36

    C++ 代码
    class Solution {
    public:
    vector rec;
    vector
    reversePrint(ListNode* head)
    {
    while (head!=NULL)
    {
    rec.push_back(head->val);
    head=head->next;
    }
    reverse(rec.begin(), rec.end());
    return rec;
    }
    };

    0 赞同 0 条回复
  • 今天你刷题了嘛 普通 2023年7月8日 下午9:34
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        ArrayList<Integer> temp = new ArrayList<Integer>();
        public int[] reversePrint(ListNode head) {
            // 单链表只能从前往后遍历
            // 需要使用栈或者递归
    
            // 进行递归,一直到尾结点
            // 回溯时添加元素
            recur(head);
            int[] res = new int[temp.size()];
            for(int i=0;i<res.length;i++){
                res[i] = temp.get(i);
            }
            return res;
    
        }
        void recur(ListNode head){
            // 终止条件
            if(head == null) return;
            // 递归
            recur(head.next);
            // 回溯[head是传入进行的结点]
            temp.add(head.val);
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • 醒醒醒醒 普通 2023年7月8日 下午9:33
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> reversePrint(ListNode* head) {
        if (head == nullptr){
            return {};
        }
    
        int count = 0;
        ListNode* p = head;
        while (p){
            count++;
            p = p->next;
        }
    
        vector<int> res(count);
        int k = count - 1;
        while (head){
            res[k--] = head->val;
            head = head->next;
        }
        return res;
        }
    };
    

    创建数组,从右往左填充数组

    0 赞同 0 条回复
  • 北辰 普通 2023年7月8日 下午8:31
    public int[] reversePrint(ListNode head) {
            ListNode p = head;
            int k=0;
            while(p!=null) {
                k++;
                p = p.next;
            }
            p=head;
            int[] res = new int[k];
            while(p!=null) {
                res[--k] = p.val;
                p = p.next;
            }
            return res;
        }
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • 土豆芋泥包_ 普通 2023年7月8日 下午8:14

    代码如下:

    class Solution {
        public int[] reversePrint(ListNode head) {
            if(head==null){
                return new int[0];
            }
            int count=0;
            ListNode temp = head;
            while(temp!=null){
                count++;
                temp = temp.next;
            }
            int[] res = new int[count];
            int i = count-1;
            while(head!=null){
                res[i--] = head.val;
                head = head.next;
            }
            return res;
        }
    }
    
    0 赞同 0 条回复
  • 阿尚 普通 2023年7月8日 下午7:53
    class Solution {
        public int[] reversePrint(ListNode head) {
            List resList = new LinkedList<>();
    
            process(head, resList);
    
            int[] result = new int[resList.size()];
            for(int i = 0; i < resList.size(); i++){
                result[i] = resList.get(i);
            }
            return result;
        }
    //递归
        public void process(ListNode head, List resList){
            if(head == null) return;
    
            process(head.next, resList);//递归,元素入栈
    
            resList.add(head.val);//递归返回时添加元素,相当于出栈
        }
    }
    
    0 赞同 0 条回复
  • 桎梏 普通 2023年7月8日 下午7:51
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int[] reversePrint(ListNode head) {
    
            ListNode cur = head;
            int len = 0;
            while(cur != null) {
                len++;
                cur = cur.next;
            }
            cur = head;
            int[] result = new int[len];
            for(int i=result.length-1; i>=0; i--) {
                result[i] = cur.val;
                cur = cur.next;
            }
            return result;
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • CaCO3 永久会员 2023年7月8日 下午7:48

    ”’java
    class Solution {
    public int[] reversePrint(ListNode head) {
    List list = new ArrayList<>();
    while(head != null){
    list.add(head.val);
    head = head.next;
    }
    int[] re = new int[list.size()];
    for(int i = 0;i < list.size();i++){
    re[i] = list.get(list.size()-1-i);
    }
    return re;
    }
    }
    ”’
    时间复杂度O(n)
    空间复杂度O(n)

    0 赞同 0 条回复
  • 朱志刚 普通 2023年7月8日 下午6:18

    ”C++
    class Solution {
    public:
    vector reversePrint(ListNode* head) {
    if(headNULL)return {};
    int count=0;
    ListNode* temp=head;
    for(int i=0;temp!=NULL;i++){count++;temp=temp->next;}
    vector ans(count);
    while(head)
    {
    ans[–count]=head->val;
    head=head->next;
    }

        return ans;
    
    }
    

    };
    ”
    内存消耗:
    8.3 MB

    0 赞同 0 条回复
  • 旺旺旺 普通 2023年7月8日 下午6:09

    Definition for singly-linked list.

    class ListNode(object):

    def init(self, x):

    self.val = x

    self.next = None

    #递归
    class Solution(object):
    def reversePrint(self, head):
    “””
    :type head: ListNode
    :rtype: List[int]
    “””
    return self.reversePrint(head=head.next) + [head.val] if head else []

    #栈
    class Solution(object):
    def reversePrint(self, head):
    stack=[]
    while head:
    stack.append(head.val)
    head=head.next
    return stack[::-1]

    class Solution(object):
    def reversePrint(self, head):
    stack=[]
    while head:
    stack.append(head.val)
    head=head.next

        res=[]
        while stack:
            res.append(stack.pop())
        return res
    

    #逆向赋值
    class Solution(object):
    def reversePrint(stack, head):
    len=0
    temp=head
    while temp:
    len+=1
    temp=temp.next

        res=[0]*len
        k=len-1
        while head:
            res[k]=head.val
            k-=1
            head=head.next
        return res
    
    0 赞同 0 条回复
  • 小季同学 普通 2023年7月8日 下午5:23

    递归

    function reversePrint(head: ListNode | null): number[] {
        if(!head) return []
        const result: number[] = []
    
        function reverse(node: ListNode) {
            if(!node) return
            reverse(node.next)
            result.push(node.val)
        }
        reverse(head)
        return result
    };
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月8日 下午5:02

    时间复杂度:O(n)
    空间复杂度:O(n)

    class Solution {
        public int[] reversePrint(ListNode head) {
            List<Integer> list=new ArrayList<>();
            while(head!=null){
                list.add(head.val);
                head=head.next;
            }
            int[] res=new int[list.size()];
            for(int i=0;i<list.size();i++){
                res[i]=list.get(list.size()-1-i);
            }
            return res;
        }
    }
    
    0 赞同 0 条回复
  • Luminous 普通 2023年7月8日 下午4:30
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        List<Integer> res=new ArrayList<Integer>();    
        public void reverse(ListNode p){
            if(p!=null){
                if(p.next!=null)
                    reverse(p.next);
                res.add(p.val);
            }
        }
        public int[] reversePrint(ListNode head) {
            reverse(head);
            //将list转为int[]
            int[] ans=new int[res.size()];
            for(int i=0;i<res.size();i++){
                ans[i]=res.get(i);
            }
            return ans;
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • 一琬人间烟火 普通 2023年7月8日 下午3:44

    使用反向迭代器效率会高些c++
    ”’
    class Solution {
    public:
    vector reversePrint(ListNode* head) {
    ListNode* dnode=head;
    int s=0;
    while(dnode!=NULL){
    s++;
    dnode=dnode->next;
    }
    vector
    ans(s);
    dnode=head;
    for(auto i=ans.rbegin();i!=ans.rend();i++){
    *i =dnode->val;
    dnode=dnode->next;
    }
    return ans;

    }
    

    };
    ”’

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月8日 下午3:43
    class Solution {
        public int[] reversePrint(ListNode head) {
            // 输入:head = [1,3,2]
            //输出:[2,3,1]
            Stack<Integer> stack = new Stack<>();
            int length = 0;
            while (head!=null) {
                stack.push(head.val);
                length++;
                head = head.next;
            }
            int[] res = new int[length];
            length = 0;
            while (!stack.isEmpty()) {
                res[length++] = stack.pop();
            }
            return res;
        }
    }
    
    0 赞同 0 条回复
  • ONE PIECE 普通 2023年7月8日 下午3:09
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int[] reversePrint(ListNode head) {
            if(head==null) return new int[]{};
            List<Integer> list = new ArrayList<>();
            ListNode p = head;
            while(p!=null){
                list.add(p.val);
                p=p.next;
            }
            int n = list.size();
            int[] res = new int[n];
            for(int i =0;i<n;i++){
                res[i] = list.get(n-i-1);
            }
            return res;
        }
    }
    

    时间复杂度:O(2n)
    空间复杂度:O(2n)

    0 赞同 0 条回复
  • Terence💧 普通 2023年7月8日 下午3:01
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func reversePrint(head *ListNode) []int {
        var ret []int
        var pre, cur *ListNode = nil, head
        for cur != nil {
            next := cur.Next
            cur.Next = pre
            pre = cur
            cur = next
        }
        for pre != nil {
            ret = append(ret, pre.Val)
            pre = pre.Next
        }
    
        return ret
    }
    

    时间复杂度On

    0 赞同 0 条回复
  • 长河呼叫黄江 普通 2023年7月8日 下午2:20

    //建一个stack
    /*为什么在第二个for循环中不能使用for(int i = 0; i < stack.size(); i++)而要使用for(int i = 0; i < res.length; i++)。
    这是因为在每次循环中,我们都会从栈中弹出元素,并将其存储在数组res中。在每次循环之后,栈的大小会减少,但是我们需要对数组res进行正确的遍历。
    如果我们使用for(int i = 0; i < stack.size(); i++)作为循环条件,那么在每次循环中,都会重新计算栈的大小。由于每次循环栈的大小都在减小,循环条件就会变得不准确,导致可能会漏掉一些元素或者访问到不存在的索引位置,从而导致错误。
    相反,如果我们使用for(int i = 0; i < res.length; i++)作为循环条件,由于数组res的大小是提前确定的,并且与栈的大小一致,所以循环条件始终准确无误。这样可以确保我们能够正确地遍历整个数组并获取所有栈中的元素。
    */

    class Solution {
        public int[] reversePrint(ListNode head) {
            LinkedList<Integer> stack = new LinkedList<>();
            while(head != null){
                stack.addLast(head.val);
                head = head.next;
            }
            int[] res = new int[stack.size()];
            for(int i = 0; i < res.length; i ++){
                res[i] = stack.removeLast();
            }
            return res;
        }
    }
    
    0 赞同 0 条回复
  • 波雅汉库克 普通 2023年7月8日 下午2:06

    1.递归

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        void addValfromBack(ListNode* cur, vector<int>& res){
            if(cur == nullptr){
                return;
            }
            addValfromBack(cur->next, res);
            res.push_back(cur->val);
        }
        vector<int> reversePrint(ListNode* head) {
            vector<int> res;
            addValfromBack(head, res);
            return res;
        }
    };
    

    时间:O(n)
    空间:O(n)

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        vector reversePrint(ListNode* head) {
            if(head == nullptr){
                return {};
            }
            if(head->next == nullptr){
                return {head->val};
            }
    
            ListNode* prev = nullptr;
            ListNode* cur = head;
            while(cur!=nullptr){
                ListNode* next = cur->next;
                cur->next = prev;
                prev = cur;
                cur = next;
            }
    
            vector res;
            while(prev!=nullptr){
                res.push_back(prev->val);
                prev = prev->next;
            }
            return res;
        }
    };
    

    时间:O(n)
    空间:O(1)

    0 赞同 0 条回复

# 回答此问题

后才能回答