首页 > 编程语言 > 详细

483.链表转数组

时间:2019-04-02 23:07:05      阅读:137      评论:0      收藏:0      [点我收藏+]

Convert Linked List to Array List

Description

Convert a linked list to an array list.
将一个链表转换为一个数组。

Example

Given 1->2->3->null, return [1,2,3].

/**
 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param head: the head of linked list.
     * @return: An integer list
     */
    public List<Integer> toArrayList(ListNode head) {
        // write your code here
        // List<Integer> res = new ArrayList<Integer>();
        // if(head != null){
        //     for(int i=head.val; i>0; i--){
        //         res.add(i);
        //     }
        // }
        // return res;
        List<Integer> res = new ArrayList<Integer>();
        while(head != null){
            res.add(head.val);
            head = head.next;
        }
        return res;
    }
}

483.链表转数组

原文:https://www.cnblogs.com/browselife/p/10646003.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!