链表初始化
typedef 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) {}
}*LinkList,ListNode;
创建链表
LinkList CreateList(const vector<int>& nums) {
ListNode* head = new ListNode;
ListNode* cur = head;
for(const int& i : nums) {
ListNode* node = new ListNode(i);
cur->next = node;
cur = cur->next;
}
return head;
}
链表转数组
vector<int> ListToNum(LinkList head) {
vector<int> num;
ListNode* cur = head;
while(cur->next != nullptr) {
num.emplace_back(cur->next->val);
cur = cur->next;
}
return num;
}
list
template<class T>
void Print(const list<T>& my)
{
typename list<T>::const_iterator it = my.begin();
for (; it != my.end(); it++)
{
cout << *it << "\t";
}
cout << endl;
}