首页 > 其他分享 >剑指 Offer 06. 从尾到头打印链表

剑指 Offer 06. 从尾到头打印链表

时间:2023-09-03 21:35:56浏览次数:38  
标签:head 06 Offer int res return 链表 ListNode stack


剑指 Offer 06. 从尾到头打印链表

方法一

顺序添加,再翻转。

class Solution {
    public int[] reversePrint(ListNode head) {
        ListNode h = head;
        List<Integer> res = new ArrayList<>();
        while(h != null){
            res.add(h.val);
            h = h.next;
        }
        Collections.reverse(res);
        return res.stream().mapToInt(x -> x).toArray();
    }
}

方法二

利用栈后进先出的特性。

class Solution {
    public int[] reversePrint(ListNode head) {
        ListNode h = head;
        Deque<Integer> stack = new ArrayDeque<>();
        while(h != null){
            stack.push(h.val);
            h = h.next;
        }
        int[] res = new int[stack.size()];
        int k = 0;
        while(!stack.isEmpty()){
            res[k++] = stack.pop();
        }

        return res;
    }
}

方法三

递归

class Solution {
    List<Integer> res = new ArrayList<>();

    public int[] reversePrint(ListNode head) {
        dfs(head);
        return res.stream().mapToInt(x -> x).toArray();
    }

    void dfs(ListNode head){
        if(head == null) return;
        dfs(head.next);
        res.add(head.val);
    }
}


标签:head,06,Offer,int,res,return,链表,ListNode,stack
From: https://blog.51cto.com/u_16208057/7343328

相关文章

  • 剑指 Offer 03. 数组中重复的数字
    剑指Offer03.数组中重复的数字利用题目的限制条件:所有数字都在0~n-1的范围内通过交互让数字和下标一一对应,如果有多个数字对应同一个下标,那就找到了答案。classSolution{publicintfindRepeatNumber(int[]nums){intn=nums.length;inti=0;......
  • 链表
    链表当需要插入或删除数据结构中某些元素时,用链表比数组方便得多,访问某一元素时则链表就是个dd。(链表可以用来秀指针操作注意:链表中的每个元素在使用前都要申请一个空间(new)并指向NULL(->next=NULL),即初始化操作由于链表不能访问某一元素,所以每次操作前都要从头开始(p=h......
  • 单链表题目*4
    //获取单链表有效结点个数publicstaticintgetLength(ListNodehead){if(head.next==null){return0;}intresult=0;ListNodetemp=head.next;while(temp!=null){result++;temp=temp.next;}returnresult;}//......
  • 剑指 Offer 62. 圆圈中最后剩下的数字(简单)
    题目:classSolution{public:intlastRemaining(intn,intm){intpos=0;for(inti=2;i<=n;i++){pos=(pos+m)%i;}returnpos;}};作者:想吃火锅的木易链接:https://leetcode.cn/problems/yuan-quan-zhong-z......
  • day06
    预处理指令:  #define  常见笔试面试题:  1、简述#define与typedef的区别:    如果是普通类型,它们在功能上无任何区别,但本质不同,一个是代码替换,一个是类型重定义    #defineINTPint*      INTPp1,p2,p3; //p1是指针p2p3是int......
  • 剑指offer_20230803
    剑指Offer51.数组中的逆序对题目说明在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。解题思路1:暴力肯定是可行但是会超时的,就不用考虑了,但理论可行解题思路2:归并可以利用归并排序时的一个特性......
  • 剑指 Offer 57 - II. 和为s的连续正数序列(简单)
    题目:classSolution{public:vector<vector<int>>findContinuousSequence(inttarget){//本题使用滑动窗口(双指针)inti=1,j=1;//定义左右边界,一般是左闭右开intsum=0;//窗口内的和vector<vector<int>>result;whi......
  • 剑指 Offer 39. 数组中出现次数超过一半的数字(简单)
    题目:classSolution{public:intmajorityElement(vector<int>&nums){unordered_map<int,int>map;intresult;for(inti=0;i<nums.size();i++){map[nums[i]]++;}for(inti=0;i<n......
  • 链表实现插入排序
    将一串整型范围内的数按升序输出。数据输入样式16-53-3-54744818-245463663-9919-99990789用链表实现实例:1)结构体定义1structListNode2{3intdata;4ListNode*next;//结构体指针5ListNode*pre;//结构体指针6};2)建一个初始......
  • Leetcode 剑指 Offer 58 - II. 左旋转字符串(Zuo xuan zhuan zi fu chuan lcof)
    题目链接字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。示例1:输入:s="abcdefg",k=2输出:"cdefgab"示例2:输入:s=......