Java offer list wins the print head from the tail

Wins the print head from the tail offer list

The most straightforward idea is to get away after the inverted output of the list, but some of them look the interviewer will say that you destroy the structure of the list, how to accomplish without damage linked list structure

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

The idea is treated with a built-in dynamic array arraylist add method, format add (x, value), x is the position in the array, each array is inserted in the 0 position, and then sequentially traversing the list in reverse order of the array structure completed .

Published 13 original articles · won praise 0 · Views 844

Guess you like

Origin blog.csdn.net/kelexing4/article/details/105304945