剑指Offer-链表-(1)

知识点:链表中的指针 数据结构:链表

题目:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

*思路《剑指Offer》:看到这道题后,第一反应把链表中链接节点的指针反转过来,改变链表的方向,然后就可以从尾到头输出了。但是该方法会改变原来链表的数据结构。

下面的代码是新建了一个链表,最简单直接的办法。

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList <Integer>  list= new  ArrayList <Integer>();
        while(listNode!=null){
            list.add(0,listNode.val);
            listNode=listNode.next;//没有明白打印这个功能体现在了哪里
        }
        return list ;
     }
}

通常打印是一个只读操作,我们不希望打印的时候修改内容;
下面开始遍历链表。遍历的舒徐是从头到尾,可是输出的顺序确是从尾到头,这是典型的“后见先出”,我们可以利用栈实现这种方式:具体过程为,没经过一个节点的时候,把该节点放到一个栈中。当遍历完整个链表后,再从栈顶逐个输出节点的值,此时输出的节点顺序就反转过来了。*

上述第一种思路就是模拟了栈的过程。
下面是栈的源码:

/*
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
 */

package java.util;

/**
 * The {@code Stack} class represents a last-in-first-out
 * (LIFO) stack of objects. It extends class {@code Vector} with five
 * operations that allow a vector to be treated as a stack. The usual
 * {@code push} and {@code pop} operations are provided, as well as a
 * method to {@code peek} at the top item on the stack, a method to test
 * for whether the stack is {@code empty}, and a method to {@code search}
 * the stack for an item and discover how far it is from the top.
 * <p>
 * When a stack is first created, it contains no items.
 *
 * <p>A more complete and consistent set of LIFO stack operations is
 * provided by the {@link Deque} interface and its implementations, which
 * should be used in preference to this class.  For example:
 * <pre>   {@code
 *   Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
 *
 * @author  Jonathan Payne
 * @since   1.0
 */
public
class Stack<E> extends Vector<E> {
    /**
     * Creates an empty Stack.
     */
    public Stack() {
    }

    /**
     * Pushes an item onto the top of this stack. This has exactly
     * the same effect as:
     * <blockquote><pre>
     * addElement(item)</pre></blockquote>
     *
     * @param   item   the item to be pushed onto this stack.
     * @return  the {@code item} argument.
     * @see     java.util.Vector#addElement
     */
    public E push(E item) {
        addElement(item);

        return item;
    }

    /**
     * Removes the object at the top of this stack and returns that
     * object as the value of this function.
     *
     * @return  The object at the top of this stack (the last item
     *          of the {@code Vector} object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E pop() {
        E       obj;
        int     len = size();

        obj = peek();
        removeElementAt(len - 1);

        return obj;
    }

    /**
     * Looks at the object at the top of this stack without removing it
     * from the stack.
     *
     * @return  the object at the top of this stack (the last item
     *          of the {@code Vector} object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }

    /**
     * Tests if this stack is empty.
     *
     * @return  {@code true} if and only if this stack contains
     *          no items; {@code false} otherwise.
     */
    public boolean empty() {
        return size() == 0;
    }

    /**
     * Returns the 1-based position where an object is on this stack.
     * If the object {@code o} occurs as an item in this stack, this
     * method returns the distance from the top of the stack of the
     * occurrence nearest the top of the stack; the topmost item on the
     * stack is considered to be at distance {@code 1}. The {@code equals}
     * method is used to compare {@code o} to the
     * items in this stack.
     *
     * @param   o   the desired object.
     * @return  the 1-based position from the top of the stack where
     *          the object is located; the return value {@code -1}
     *          indicates that the object is not on the stack.
     */
    public synchronized int search(Object o) {
        int i = lastIndexOf(o);

        if (i >= 0) {
            return size() - i;
        }
        return -1;
    }

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = 1224463164541339165L;
}

基于上述思路的改变:

想到了用栈来实现这个函数,而递归在本质上就是一个栈结构,于是想到了用递归来实现。

import java.util.ArrayList;
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;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35649064/article/details/84430619