"Sword Finger Offer" 05: Print the linked list from end to beginning

❝
首先学习计算机科学及理论。接着形成自己编程的风格。然后把这一切都忘掉,尽管改程序就是了。—— 小浩

❞

Print linked list from end to beginning


Title description

Enter a linked list and return an ArrayList in the order of the linked list values ​​from end to beginning.

Original title

"Sword Finger Offer" 05: Print the linked list from end to beginning

solution

Solution 1 [Recommended]
Traverse the linked list, push the value of each linked list node into the stack, and finally pop the elements in the stack into the list in turn.


/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
import java.util.Stack;

public class Solution {
    /**
     * 从尾到头打印链表
     * @param listNode 链表头节点
     * @return list
     */
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> res = new ArrayList<>();
        if (listNode == null) {
            return res;
        }
        Stack<Integer> stack = new Stack<>();
        while (listNode != null) {
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        while (!stack.isEmpty()) {
            res.add(stack.pop());
        }

        return res;
    }
}

Solution two [not recommended]
Use recursion:

If it is not the end node of the linked list, continue the recursion;
if it is, add it to the list.
This method is not recommended. When there are too many recursive layers, Stack Overflow is prone to occur.

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
import java.util.Stack;

/**
 * @author bingo
 * @since 2018/10/28
 */
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> res = new ArrayList<>();
        if (listNode == null) {
            return res;
        }

        addElement(listNode, res);
        return res;

    }

    private void addElement(ListNode listNode, ArrayList<Integer> res) {
        if (listNode.next != null) {
            // 递归调用
            addElement(listNode.next, res);
        }
        res.add(listNode.val);
    }
}

Idea expansion

"Sword Finger Offer" 05: Print the linked list from end to beginning

Test case

  1. Functional test (the input linked list has multiple nodes; the input linked list has only one node);
  2. Special input test (input linked list node pointer is empty).

    Test site for this question

    "Sword Finger Offer" 05: Print the linked list from end to beginning
    I organized all the solutions I wrote into an e-book on github, and hit the top of the github rankings in three days! Nearly 5w people downloaded and read! If you want to get it, just enter the link below (remember to give me a star) :

https://github.com/geekxh/hello-algorithm

"Sword Finger Offer" 05: Print the linked list from end to beginning

Guess you like

Origin blog.51cto.com/15076236/2609611