LeetCode-print linked list from end to beginning

Title description

Problem-solving ideas

  • First traverse the linked list, and store each value in the linked list in an array.
  • Then flashback to traverse this array, and finally return

Implementation code

var reversePrint = function(head) {
    
    
    const arr = [];
    while (head) {
    
    
        arr.push(head.val);
        head = head.next;
    }

    const result = [];
    const len = arr.length;
    for (let i = 0; i < len;i++) {
    
    
        result.push(arr.pop());
    }

    return result;

};

Guess you like

Origin blog.csdn.net/sinat_41696687/article/details/115020535