// LeetCode 的链表结构体
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr)
{
}
ListNode(int x) : val(x), next(nullptr)
{
}
ListNode(int x, ListNode *next) : val(x), next(next)
{
}
};
ListNode *createList(const vector<int> &nums)
{
ListNode *L = new ListNode();
ListNode *T = L;
for (int i = 0; i < nums.size(); i++)
{
ListNode *temp = new ListNode();
temp->val = nums[i];
temp->next = nullptr;
T->next = temp;
T = T->next;
}
ListNode *temp = L;
L = L->next;
delete temp;
return L;
}
int main()
{
vector<int> nums = {1, 2, 4, 5, 6, 7};
// 创建链表
ListNode *L = createList(nums);
// 输出链表
while (L)
{
cout << L->val << " ";
L = L->next;
}
return 0;
}
LeetCode 构建链表(C++)
于 2023-11-01 20:02:31 首次发布