Swordfinger offer printListFromTailToHead prints the linked list from end to end

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; 
}

doesn"t understand the meaning of this in the code, and arrayList.add (listNode.val); add a value that you don"t know about this node? Why is it the value an of the next node? Ask for the answer of the Great God

Mar.18,2021

this represents the current object, the printListFromTailToHead method is called to create the Solution object, and the this points to the Solution object, which is available or not in this code. The recursive code executes in the order of traversing to the last ListNode,ArrayList and then from the last ListNodeadd its val.
for example 7-> 8-> 9-> 10, traversing to 10, then 10add into list, and then add9, so printing the linked list from end to end.

Menu