2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2023-04-24 04:11:18 +08:00
|
|
|
* File: list_node.hpp
|
2022-11-25 02:04:38 +08:00
|
|
|
* Created Time: 2021-12-19
|
2024-02-07 22:21:18 +08:00
|
|
|
* Author: krahets (krahets@163.com)
|
2022-11-25 02:04:38 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iostream>
|
2023-07-19 16:09:27 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2022-11-25 02:04:38 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
/* 链表节点 */
|
2022-11-25 02:04:38 +08:00
|
|
|
struct ListNode {
|
|
|
|
int val;
|
|
|
|
ListNode *next;
|
2023-04-14 03:44:02 +08:00
|
|
|
ListNode(int x) : val(x), next(nullptr) {
|
|
|
|
}
|
2022-11-25 02:04:38 +08:00
|
|
|
};
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
/* 将列表反序列化为链表 */
|
2023-04-14 03:44:02 +08:00
|
|
|
ListNode *vecToLinkedList(vector<int> list) {
|
2022-11-25 02:04:38 +08:00
|
|
|
ListNode *dum = new ListNode(0);
|
|
|
|
ListNode *head = dum;
|
|
|
|
for (int val : list) {
|
|
|
|
head->next = new ListNode(val);
|
|
|
|
head = head->next;
|
|
|
|
}
|
|
|
|
return dum->next;
|
|
|
|
}
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
/* 释放分配给链表的内存空间 */
|
2023-01-14 19:52:11 +08:00
|
|
|
void freeMemoryLinkedList(ListNode *cur) {
|
|
|
|
// 释放内存
|
|
|
|
ListNode *pre;
|
|
|
|
while (cur != nullptr) {
|
|
|
|
pre = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
delete pre;
|
|
|
|
}
|
|
|
|
}
|