题解 | 序列链表化

序列链表化

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;
    }
};

全部评论

相关推荐

02-04 12:01
九江学院 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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