将一个链表转换为一个数组。
样例
样例 1:
输入: 1->2->3->null
输出: [1,2,3]
样例 2:
输入: 3->5->8->null
输出: [3,5,8]
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: the head of linked list.
* @return: An integer list
*/
vector<int> ret;
vector<int> toArrayList(ListNode * head)
{
// write your code here
if(head == NULL)
return ret;
while(head)
{
ret.push_back(head->val);
head=head->next;
}
return ret;
}
};