zoukankan      html  css  js  c++  java
  • 剑指 Offer 06. 从尾到头打印链表

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

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

    问题描述:

    输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

    示例 1:

    输入:head = [1,3,2]
    输出:[2,3,1]

    限制:

    0 <= 链表长度 <= 10000

    /**
     * Definition for singly-linked list.
     * class ListNode(var _x: Int = 0) {
     *   var next: ListNode = null
     *   var x: Int = _x
     * }
     */
    //存储结果 然后翻转
    //也可以使用栈
    //也可以翻转链表
    // pre cur = head
    // temp = cur.next
    // cur.next = pre
    // pre = cur
    // cur = cur.next
    object Solution {
        def reversePrint(head: ListNode): Array[Int] = {
            var newHead = head
            var ans = new Array[Int](0)
            if (head == null) return ans
    
            while (newHead != null) {
                ans = ans.prepended(newHead.x)
                newHead = newHead.next
                //ans = ans.prepended(newHead.x)
            }
            return ans
        }
    }
    
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    //go stack 利用slice实现
    func reversePrint(head *ListNode) []int {
        if head == nil {return nil}
    
        res := make([]int, 0)
    
        for (head != nil) {
            res = append(res, head.Val)
            head = head.Next
        }
    
        for i, j := 0, len(res)-1; i <= j; i, j = i+1, j-1 {
            temp := res[i]
            res[i] = res[j]
            res[j] = temp
        } 
    
        return res
    }
    
  • 相关阅读:
    MySQL--mysqldump的权限说明
    JS--switch 语句
    psutil官方文档
    mysql 性能分析套件
    mysql 在启动时配置文件的查找方式
    mysql中的意向锁IS,IX
    mysql innodb_double_write特性
    mysql sql_mode 之 NO_ENGINE_SUBSTITUTION
    编译安装hph
    pip list 和 pip freeze
  • 原文地址:https://www.cnblogs.com/ganshuoos/p/14141519.html
Copyright © 2011-2022 走看看