Convert Linked List to Array List

题目:

Convert a linked list to an array list.

分析:

解法:

public class Solution {
    /**
     * @param head the head of linked list.
     * @return an array list
     */
    public List<Integer> toArrayList(ListNode head) {  
        // Write your code here
        List<Integer> res = new ArrayList<Integer>();
        while (head != null) {
            res.add(head.val);
            head = head.next;
        }
        return res;
    }
}

results matching ""

    No results matching ""