Detailed Explanation of Question 3 of Sword Finger Offer (with Java and Python source code)

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

1. Java implementation

1.1 addFirst insert

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    
    
    ArrayList<Integer> list=new ArrayList<>();
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    
    
        while (listNode!=null) {
    
    
            list.add(0,listNode.val);
            listNode=listNode.next;
        }
        return list;
    }
}

1.2 Recursion

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    
    
    ArrayList<Integer> list=new ArrayList<>();
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    
    
        if (listNode!=null) {
    
    
            printListFromTailToHead(listNode.next);
            list.add(listNode.val);
        }
        return list;
    }
}

2. Python implementation

2.1 Using the stack

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        stack = []
        result = []
        while listNode:
            stack.append(listNode.val)
            listNode = listNode.next
        while stack:
            result.append(stack.pop())
        return result

2.2 Use recursion

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        list = []
        def fun(Node):
            if Node:
                fun(Node.next)
                list.append(Node.val)
        fun(listNode)
        return list

2.3 Reverse order after traversing from beginning to end

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        list = []
        while listNode:
            list.append(listNode.val)
            listNode=listNode.next
        return list[::-1]

2.4 Stack + recursion

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def __init__(self):
        self.list=[]
    def printListFromTailToHead(self, listNode):
        if listNode:
            if listNode.next:
                self.printListFromTailToHead(listNode.next)
            self.list.append(listNode.val)
        return self.list

Guess you like

Origin blog.csdn.net/weixin_44285445/article/details/108088762