题解 | 序列链表化

序列链表化

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
        if(arr.size()==0)return nullptr;
        auto head=new ListNode(arr[0]);
        auto cur=head;
        for(int i=1;i<arr.size();i++){
            cur->next=new ListNode(arr[i]);
            cur=cur->next;
        }
        return head;
    }
};

全部评论

相关推荐

03-28 16:43
佛山大学 Java
java全国可飞:简历2.0,各位佬看看,这样可以吗查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务