题解 | 序列链表化
序列链表化
https://www.nowcoder.com/practice/a407f7e495084084b1e1f628dfd769fd
/**
* 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 *head=new ListNode(0);
ListNode *res=head;
int len=arr.size();
if(len==0) return nullptr;
for(int i=0;i<len;i++){
head->val=arr[i];
if(i==len-1) continue;
ListNode *temp=new ListNode(0);
head->next=temp;
head=temp;
}
return res;
}
};
OPPO公司福利 1225人发布