链表节点:
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) {}
};
构建方法:
int main()
{
vector<int> vec = {1,2,2,1};
ListNode *head;
if (vec.size() > 0)
{
head = new ListNode(vec[0]);
ListNode *last_node = new ListNode();
for (int i = 1; i < vec.size(); i++)
{
if (i == 1)
{
head->next = new ListNode(vec[i]);
last_node = head->next;
}
else
{
last_node->next = new ListNode(vec[i]);
last_node = last_node->next;
}
}
}
else head=nullptr;
// ListNode *next_node = head;
// while (next_node != nullptr)
// {
// cout << next_node->val;
// next_node = next_node->next;
// }
}