/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param arr int整型vector
* @return ListNode类
*/
ListNode* vectorToListnode(vector<int>& arr) {
// write code here
ListNode* dummy = new ListNode(-1);//头插法
ListNode* p = dummy;//创建p用来移动,而不用dummy来移动,因为最后要返回链表的头节点的指针
for(int i = 0; i < arr.size(); i++) {
p->next = new ListNode(arr[i]);
p = p->next;
}
return dummy->next;//dummy是虚拟节点,其next才指向arr的第一个元素
}
};