You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
819 B
43 lines
819 B
8 months ago
|
/**
|
||
|
* File: list_node.hpp
|
||
|
* Created Time: 2021-12-19
|
||
|
* Author: krahets (krahets@163.com)
|
||
|
*/
|
||
|
|
||
|
#pragma once
|
||
|
|
||
|
#include <iostream>
|
||
|
#include <vector>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
/* 鏈結串列節點 */
|
||
|
struct ListNode {
|
||
|
int val;
|
||
|
ListNode *next;
|
||
|
ListNode(int x) : val(x), next(nullptr) {
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/* 將串列反序列化為鏈結串列 */
|
||
|
ListNode *vecToLinkedList(vector<int> list) {
|
||
|
ListNode *dum = new ListNode(0);
|
||
|
ListNode *head = dum;
|
||
|
for (int val : list) {
|
||
|
head->next = new ListNode(val);
|
||
|
head = head->next;
|
||
|
}
|
||
|
return dum->next;
|
||
|
}
|
||
|
|
||
|
/* 釋放分配給鏈結串列的記憶體空間 */
|
||
|
void freeMemoryLinkedList(ListNode *cur) {
|
||
|
// 釋放記憶體
|
||
|
ListNode *pre;
|
||
|
while (cur != nullptr) {
|
||
|
pre = cur;
|
||
|
cur = cur->next;
|
||
|
delete pre;
|
||
|
}
|
||
|
}
|